When using npx in any directory not initialised with a package.json, npx stops beacause of ENOENT
package.json.
npx runs the command regardlessly, like it does in the one shipped with npm@6
$ npm i -g npm@7
$ npx jay
(anywhere that's not in a package)
If the package.json
is not available on current directory, npx
will fail with ENOENT
of package.json
.
It is okay if package.json
is just empty JSON (e.g. after we echo {}>package.json
).
Tested with node:alpine
image from Docker hub (sha256:90dfaf5
) with NPM 7.0.3 and Node.js 15.0.1.
bash-5.0# npx serve
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /home/node/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/home/node/package.json'
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/2020-10-24T02_26_28_755Z-debug.log
bash-5.0# echo {}>package.json
bash-5.0# npx serve
UPDATE AVAILABLE The latest version of `serve` is 11.3.2
ERROR: Cannot copy to clipboard: Couldn't find the required `xsel` binary. On Debian/Ubuntu you can install it with: sudo apt install xsel
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ Serving! โ
โ โ
โ - Local: http://localhost:80 โ
โ - On Your Network: http://192.168.0.1:80 โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
I confirm such behaviour on Win 10 1909 with NodeJS 15.0.1.
This is really unpleasant and undesired bug.
I have a repo with directory called examples
, where the package.json
and its corresponding node_modules
reside. Then there is a bunch of subdirectories like examples/full
or examples/simple
where I used to execute npx webpack
etc. Creating empty package.json
in each one of them just to make the npx
work again seems like a superfluous work-around.
i'm not able to reproduce this with 7.0.5, is this issue still happening for you?
i'm not able to reproduce this with 7.0.5, is this issue still happening for you?
Updating to [email protected] gives me another, but seemingly related, error, that is also fixed by making an empty package.json
0 verbose cli [
0 verbose cli 'C:\\Program Files\\nodejs\\node.exe',
0 verbose cli 'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
0 verbose cli 'exec',
0 verbose cli '--',
0 verbose cli 'serve'
0 verbose cli ]
1 info using [email protected]
2 info using [email protected]
3 timing config:load:defaults Completed in 1ms
4 timing config:load:file:C:\Program Files\nodejs\node_modules\npm\npmrc Completed in 0ms
5 timing config:load:builtin Completed in 0ms
6 timing config:load:cli Completed in 1ms
7 timing config:load:env Completed in 0ms
8 timing config:load:file:C:\.npmrc Completed in 0ms
9 timing config:load:project Completed in 1ms
10 timing config:load:file:C:\Users\nic\.npmrc Completed in 1ms
11 timing config:load:user Completed in 1ms
12 timing config:load:file:C:\Program Files\nodejs\etc\npmrc Completed in 0ms
13 timing config:load:global Completed in 1ms
14 timing config:load:cafile Completed in 0ms
15 timing config:load:validate Completed in 0ms
16 timing config:load:setUserAgent Completed in 1ms
17 timing config:load:setEnvs Completed in 1ms
18 timing config:load Completed in 7ms
19 verbose npm-session d7ae2dbc81df89e0
20 timing npm:load Completed in 19ms
21 http fetch GET 304 https://registry.npmjs.org/serve 131ms (from cache)
22 timing arborist:ctor Completed in 1ms
23 timing command:exec Completed in 150ms
24 verbose stack TypeError: could not detect node name from path or package
24 verbose stack at new Node (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\node.js:114:13)
24 verbose stack at Arborist.[newNode] (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\load-actual.js:251:9)
24 verbose stack at C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\load-actual.js:214:57
24 verbose stack at async Arborist.[loadActual] (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\load-actual.js:116:25)
24 verbose stack at async exec (C:\Program Files\nodejs\node_modules\npm\lib\exec.js:141:16)
25 verbose cwd C:\Users\nic
26 verbose Windows_NT 10.0.18363
27 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "exec" "--" "serve"
28 verbose node v15.0.1
29 verbose npm v7.0.1
30 error could not detect node name from path or package
31 verbose exit 1
the log output above shows npm 7.0.1, is that correct? you should be able to update with npm i -g npm@7
My mistake, the log output shows 7.0.1
because I reverted to 7.0.1
, before reporting back, where I also get the error like this now.
@Nicholaiii did you get this solved with a newer version? I still have this problem with the latest node (via docker node:15.1.0-alpine
)
@Nicholaiii did you get this solved with a newer version? I still have this problem with the latest node (via docker
node:15.1.0-alpine
)
No. I am using [email protected]
and [email protected]
and the issue persists. Same solution with empty package.json
solves it, but it's surely frustrating to have your workflows broken.
@MoSattler @Nicholaiii thanks for the feedback! Could you please share any log files/package.json so that we can figure out why is this problem still happening? ๐
@MoSattler @Nicholaiii thanks for the feedback! Could you please share any log files/package.json so that we can figure out why is this problem still happening? ๐
Fresh install of Node 15.1.0 and npm i -g [email protected]
, that for some (probably unrelated) reason don't work properly.
0 verbose cli [
0 verbose cli 'C:\\Program Files\\nodejs\\node.exe',
0 verbose cli 'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
0 verbose cli 'exec',
0 verbose cli '--',
0 verbose cli 'serve'
0 verbose cli ]
1 info using [email protected]
2 info using [email protected]
3 timing config:load:defaults Completed in 1ms
4 timing config:load:file:C:\Program Files\nodejs\node_modules\npm\npmrc Completed in 2ms
5 timing config:load:builtin Completed in 2ms
6 timing config:load:cli Completed in 1ms
7 timing config:load:env Completed in 0ms
8 timing config:load:file:C:\.npmrc Completed in 0ms
9 timing config:load:project Completed in 1ms
10 timing config:load:file:C:\Users\nic\.npmrc Completed in 1ms
11 timing config:load:user Completed in 1ms
12 timing config:load:file:C:\Users\nic\AppData\Roaming\npm\etc\npmrc Completed in 0ms
13 timing config:load:global Completed in 0ms
14 timing config:load:cafile Completed in 0ms
15 timing config:load:validate Completed in 1ms
16 timing config:load:setUserAgent Completed in 0ms
17 timing config:load:setEnvs Completed in 1ms
18 timing config:load Completed in 8ms
19 verbose npm-session 2b37324281b4fc0b
20 timing npm:load Completed in 16ms
21 http fetch GET 304 https://registry.npmjs.org/serve 153ms (from cache)
22 timing arborist:ctor Completed in 1ms
23 timing command:exec Completed in 183ms
24 verbose stack TypeError: could not detect node name from path or package
24 verbose stack at new Node (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\node.js:112:13)
24 verbose stack at Arborist.[newNode] (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\load-actual.js:249:9)
24 verbose stack at C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\load-actual.js:212:57
24 verbose stack at async Arborist.[loadActual] (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\load-actual.js:116:25)
24 verbose stack at async exec (C:\Program Files\nodejs\node_modules\npm\lib\exec.js:165:16)
25 verbose cwd C:\Users\nic
26 verbose Windows_NT 10.0.18363
27 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "exec" "--" "serve"
28 verbose node v15.1.0
29 verbose npm v7.0.8
30 error could not detect node name from path or package
31 verbose exit 1
There is no package.json, that's the core problem. If I make an empty one like this:
{}
it works fine.