How to Solve Error Message no such file or directory, open ‘package.json’ in NodeJS

Posted on

Introduction

The error exist as part of the title of this article is actually happens. It happen upon executing the command of ‘npm i nodemon’. The following is the command upon triggering the error message :

[root@10 crud]# npm i nodemon
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.3.1 (node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN enoent ENOENT: no such file or directory, open '/media/sf_windows/nodejs/crud/package.json'
npm WARN crud No description
npm WARN crud No repository field.
npm WARN crud No README data
npm WARN crud No license field.
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /media/sf_windows/nodejs/crud/node_modules/chalk/node_modules/supports-color/package.json.2679617430
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/media/sf_windows/nodejs/crud/node_modules/chalk/node_modules/supports-color/package.json.2679617430'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2021-03-04T10_40_27_067Z-debug.log
[root@10 crud]#

The main purpose is for installing a nodemon package. The nodemon package is very useful. As it available in this link as one of the main source of the NodeJS information. It explains that nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.

Moreover, in that link, it explains that nodemon does not require any additional changes to your code or method of development. Furthermore, nodemon is a replacement wrapper for node. Additionally, in order to use nodemon, replace the word node on the command line when executing your script. But apparently, upon installing the nodemon package, the error above appear. Normally, the error above will not appear upon the standard installation of any nodejs packages.

As an additional information, vefore starting to resolve the problem, the following is the content of the root folder of the NodeJS application :

[root@10 crud]# ls -al
total 13
drwxrwx---. 1 root vboxsf 4096 Mar  4 05:31 .
drwxrwx---. 1 root vboxsf 4096 Mar  4 05:25 ..
-rwxrwx---. 1 root vboxsf  207 Feb 27 12:18 db_config.js
drwxrwx---. 1 root vboxsf    0 Feb 10 11:29 node_modules
-rwxrwx---. 1 root vboxsf 3136 Feb 10 11:29 package-lock.json
[root@10 crud]#

Attempt for Solving Error Message no such file or directory, open ‘package.json’ in NodeJS

The node_modules folder is available after installing mysql module. But the installation of nodemon module fails.The following are the steps for solving the above error message :

1. First of all, try to initialize a NodeJS application. This will create a package.json file. The following is the execution of the command :

[root@10 crud]# npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install ` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (crud)
version: (1.0.0)
description:
entry point: (app.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /media/sf_windows/nodejs/crud/package.json:
{
  "name": "crud",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "dependencies": {
    "ansi-align": "^3.0.0",
    "braces": "^3.0.2",
    "anymatch": "^3.1.1",
    "clone-response": "^1.0.2",
    "chokidar": "^3.5.1",
    "decompress-response": "^3.3.0",
    "debug": "^3.2.7",
    "fill-range": "^7.0.1",
    "end-of-stream": "^1.4.4",
    "get-stream": "^4.1.0",
    "ignore-by-default": "^1.0.1",
    "ms": "^2.1.3",
    "minimist": "^1.2.5",
    "minimatch": "^3.0.4",
    "mimic-response": "^1.0.1",
    "mysql": "^2.18.1",
    "normalize-url": "^4.5.0",
    "normalize-path": "^3.0.0",
    "nopt": "^1.0.10",
    "once": "^1.4.0",
    "picomatch": "^2.2.2",
    "p-cancelable": "^1.1.0",
    "prepend-http": "^2.0.0",
    "pupa": "^2.1.1",
    "pump": "^3.0.0",
    "pstree.remy": "^1.1.8",
    "readdirp": "^3.5.0",
    "responselike": "^1.0.2",
    "registry-auth-token": "^4.2.1",
    "registry-url": "^5.1.0",
    "rc": "^1.2.8",
    "semver": "^5.7.1",
    "semver-diff": "^3.1.1",
    "signal-exit": "^3.0.3",
    "supports-color": "^5.5.0",
    "strip-json-comments": "^2.0.1",
    "string-width": "^4.2.2",
    "term-size": "^2.2.1",
    "strip-ansi": "^5.2.0",
    "to-regex-range": "^5.0.1",
    "type-fest": "^0.8.1",
    "to-readable-stream": "^1.0.0",
    "url-parse-lax": "^3.0.0",
    "touch": "^3.1.0",
    "typedarray-to-buffer": "^3.1.5",
    "undefsafe": "^2.0.3",
    "unique-string": "^2.0.0",
    "widest-line": "^3.1.0",
    "wrappy": "^1.0.2",
    "xdg-basedir": "^4.0.0",
    "write-file-atomic": "^3.0.3"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}
Is this OK? (yes) y
[root@10 crud]#
[root@10 crud]# ls -al
total 57
drwxrwx---. 1 root vboxsf  4096 Mar  4 05:56 .
drwxrwx---. 1 root vboxsf  4096 Mar  4 05:25 ..
-rwxrwx---. 1 root vboxsf   188 Mar  4 05:56 app.js
-rwxrwx---. 1 root vboxsf   207 Feb 27 12:18 db_config.js
drwxrwx---. 1 root vboxsf 40960 Mar  4 05:41 node_modules
-rwxrwx---. 1 root vboxsf  1703 Mar  4 05:41 package.json
-rwxrwx---. 1 root vboxsf  3136 Feb 10 11:29 package-lock.json
[root@10 crud]#

Fortunately, the execution for creating the package.json file is a success as in the above output. The full reference exist in this link. So, in the above command output, the file with the name of ‘package.json’ exist. So, it suppose it will be able to solve the error appear in the above line as follows :

npm WARN enoent ENOENT: no such file or directory, open '/media/sf_windows/nodejs/crud/package.json'

2. Apparently, after executing the command again, the problem appear with another error message as follows :

[root@10 crud]# npm i nodemon
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.3.1 (node_modules/nodemon/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN [email protected] No description
npm WARN [email protected] No repository field.
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /media/sf_windows/nodejs/crud/node_modules/cacheable-request/node_modules/pump/package.json.3495354389
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/media/sf_windows/nodejs/crud/node_modules/cacheable-request/node_modules/pump/package.json.3495354389'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2021-03-04T10_41_50_349Z-debug.log
[root@10 crud]#

3. Moreover, running the command ‘npm install’ to install the package dependencies as exist in the package.json file definition as follows is also failing :

[root@10 crud]# npm install
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.3.1 (node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN [email protected] No description
npm WARN [email protected] No repository field.
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /media/sf_windows/nodejs/crud/node_modules/string-width/node_modules/is-fullwidth-code-point/package.json.2484783542
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/media/sf_windows/nodejs/crud/node_modules/string-width/node_modules/is-fullwidth-code-point/package.json.2484783542'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2021-03-04T10_58_26_376Z-debug.log
[root@10 crud]# 

Actually, the information about the ‘npm install’ command for further reference exist in this link.

4. Next, the attempt is to reinstall the npm package as follows :

[root@10 crud]# npm install npm -g
/usr/bin/npm -> /usr/lib/node_modules/npm/bin/npm-cli.js
/usr/bin/npx -> /usr/lib/node_modules/npm/bin/npx-cli.js
+ [email protected]
added 59 packages from 24 contributors, removed 241 packages and updated 194 packages in 19.783s
[root@10 crud]#

5. Last but not least, it is an attempt directly to install the nodemon package as follows :

[root@10 crud]# npm install nodemon --save
npm ERR! code EPERM
npm ERR! syscall symlink
npm ERR! path ../is-ci/bin.js
npm ERR! dest /media/sf_windows/nodejs/crud/node_modules/.bin/is-ci
npm ERR! errno -1
npm ERR! Error: EPERM: operation not permitted, symlink '../is-ci/bin.js' -> '/media/sf_windows/nodejs/crud/node_modules/.bin/is-ci'
npm ERR!  [Error: EPERM: operation not permitted, symlink '../is-ci/bin.js' -> '/media/sf_windows/nodejs/crud/node_modules/.bin/is-ci'] {
npm ERR!   errno: -1,
npm ERR!   code: 'EPERM',
npm ERR!   syscall: 'symlink',
npm ERR!   path: '../is-ci/bin.js',
npm ERR!   dest: '/media/sf_windows/nodejs/crud/node_modules/.bin/is-ci'
npm ERR! }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2021-03-04T15_30_35_961Z-debug.log
[root@10 crud]#

In the output of the above command execution, there is a problem with the permission in the operating system. Obviously, it is because the path is a path resulting of the folder mapping from the Windows operating system in the host machine.

So, the physical folder path of ‘/media/sf_windows’ actually exist in the host machine running with a Windows operating system.

 

Another Solution for Solving the Error Message

Since the problem is not in the npm tool either the NodeJS application itself, try to check the permission in the operating system. Below are attempts for solving it :

1. First of all, just try to set the SELinux mode into a permissive mode as follows :

[root@10 crud]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Memory protection checking:     actual (secure)
Max kernel policy version:      32
[root@10 crud]# setenforce 0
[root@10 crud]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   permissive
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Memory protection checking:     actual (secure)
Max kernel policy version:      32
[root@10 crud]#

2. After that, executing the ‘npm install nodemon -save’, it is still failing as in the previous output command. So, further attempt is to try to disable the SELinux by editing the file of ‘/etc/selinux/config’ and changing the following content :

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
#SELINUX=enforcing
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

3. Next, after restarting the virtual server and then reattempt to execute the ‘npm install nodemon -save’, unfortunately it still fail as in the output execution before.

 

Final and Simple solution for Solving the Error Message

After attempting for several alternative solutions above, finally this is the last attempt. Actually, it is a simple solution. Just try to execute in another folder exist in the guest machine or directly inside the virtual server as follows for an example in the ‘/root/tmp’ folder :

[root@10 temp]# ls -al
total 4
drwxr-xr-x  2 root root  20 Mar  4 10:42 .
dr-xr-x---. 6 root root 249 Mar  4 09:07 ..
-rw-r--r--  1 root root  31 Mar  4 09:07 app.js
[root@10 temp]# npm install nodemon --save
added 119 packages, and audited 120 packages in 7s
11 packages are looking for funding
  run `npm fund` for details
found 0 vulnerabilities
[root@10 temp]# ls -al
total 92
drwxr-xr-x    3 root root    85 Mar  4 10:42 .
dr-xr-x---.   6 root root   249 Mar  4 09:07 ..
-rw-r--r--    1 root root    31 Mar  4 09:07 app.js
drwxr-xr-x  108 root root  4096 Mar  4 10:42 node_modules
-rw-r--r--    1 root root    52 Mar  4 10:42 package.json
-rw-r--r--    1 root root 78919 Mar  4 10:42 package-lock.json
[root@10 temp]#

Luckily, it works although there is only one file with the name of ‘apps.js’. Actually, the content of that file is also just one line of source ‘console.debug(“Test NodeJS”);’. So, the conclusion for solving the problem is simple.

Generally, executing npm tool in a mounted folder originally exist as a physical folder in the host machine running in Windows operating system eventually will fail. Unfortunately, no further time to seek for the reason.

2 thoughts on “How to Solve Error Message no such file or directory, open ‘package.json’ in NodeJS

Leave a Reply