Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] NPX does not work outside package directories #1975

Closed
Nicholaiii opened this issue Oct 16, 2020 · 14 comments
Closed

[BUG] NPX does not work outside package directories #1975

Nicholaiii opened this issue Oct 16, 2020 · 14 comments
Labels
Bug thing that needs fixing Release 7.x work is associated with a specific npm 7 release

Comments

@Nicholaiii
Copy link

Current Behavior:

When using npx in any directory not initialised with a package.json, npx stops beacause of ENOENT package.json.

Expected Behavior:

npx runs the command regardlessly, like it does in the one shipped with npm@6

Steps To Reproduce:

$ npm i -g npm@7
$ npx jay (anywhere that's not in a package)

Environment:

  • OS: Windows 10 1909
  • Node: 14.7.0
  • npm: 7.0.1
@Nicholaiii Nicholaiii added Bug thing that needs fixing Needs Triage needs review for next steps Release 7.x work is associated with a specific npm 7 release labels Oct 16, 2020
@compulim
Copy link

compulim commented Oct 24, 2020

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  │
   │                                              │
   └──────────────────────────────────────────────┘

@jmacura
Copy link

jmacura commented Oct 25, 2020

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.

@nlf
Copy link
Contributor

nlf commented Oct 27, 2020

i'm not able to reproduce this with 7.0.5, is this issue still happening for you?

@Nicholaiii
Copy link
Author

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

@nlf
Copy link
Contributor

nlf commented Oct 27, 2020

the log output above shows npm 7.0.1, is that correct? you should be able to update with npm i -g npm@7

@Nicholaiii
Copy link
Author

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.

image

ruyadorno added a commit to ruyadorno/cli that referenced this issue Oct 29, 2020
This fixes running `npm exec` in folders that does not have a
`package.json` file.

Fixes: npm#1975
@ruyadorno ruyadorno removed the Needs Triage needs review for next steps label Oct 29, 2020
@MoSattler
Copy link

@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
Copy link
Author

@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.

@ruyadorno
Copy link
Contributor

@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? 😊

@Nicholaiii
Copy link
Author

Nicholaiii commented Nov 10, 2020

@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.

@MoSattler
Copy link

MoSattler commented Nov 25, 2020

@ruyadorno Same problem for me as for @Nicholaiii

I use node in docker.

# Dockerfile
FROM node:15.3.0
RUN npx cowsay Hi!

Output when building is

npm ERR! could not detect node name from path or package

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-11-25T20_16_16_359Z-debug.log
Complete log
0 verbose cli [
0 verbose cli   '/usr/local/bin/node',
0 verbose cli   '/usr/local/lib/node_modules/npm/bin/npm-cli.js',
0 verbose cli   'exec',
0 verbose cli   '--',
0 verbose cli   'cowsay',
0 verbose cli   'Hi!'
0 verbose cli ]
1 info using [email protected]
2 info using [email protected]
3 timing config:load:defaults Completed in 2ms
4 timing config:load:file:/usr/local/lib/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 1ms
8 timing config:load:file:/.npmrc Completed in 0ms
9 timing config:load:project Completed in 0ms
10 timing config:load:file:/root/.npmrc Completed in 0ms
11 timing config:load:user Completed in 0ms
12 timing config:load:file:/usr/local/etc/npmrc Completed in 0ms
13 timing config:load:global Completed in 0ms
14 timing config:load:cafile Completed in 1ms
15 timing config:load:validate Completed in 0ms
16 timing config:load:setUserAgent Completed in 0ms
17 timing config:load:setEnvs Completed in 1ms
18 timing config:load Completed in 6ms
19 verbose npm-session 48fefec9b9e2616a
20 timing npm:load Completed in 12ms
21 http fetch GET 200 https://registry.npmjs.org/npm 239ms
22 http fetch GET 304 https://registry.npmjs.org/npm 69ms (from cache)
23 http fetch GET 200 https://registry.npmjs.org/cowsay 55ms
24 timing arborist:ctor Completed in 3ms
25 timing command:exec Completed in 68ms
26 verbose stack TypeError: could not detect node name from path or package
26 verbose stack     at new Node (/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/node.js:113:13)
26 verbose stack     at Arborist.[newNode] (/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/load-actual.js:249:9)
26 verbose stack     at /usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/load-actual.js:212:68
26 verbose stack     at async Arborist.[loadActual] (/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/load-actual.js:116:25)
26 verbose stack     at async exec (/usr/local/lib/node_modules/npm/lib/exec.js:165:16)
27 verbose cwd /
28 verbose Linux 5.8.18-200.fc32.x86_64
29 verbose argv "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/bin/npm-cli.js" "exec" "--" "cowsay" "Hi!"
30 verbose node v15.3.0
31 verbose npm  v7.0.14
32 error could not detect node name from path or package
33 verbose exit 1
In node 14, it works without a problem.

@MoSattler
Copy link

One thing I noticed is that npx now asks you if you want to install a package before it executes it before. I dont think that was the case before.
Maybe, in my case, this interactivity doesnt play along nicely with docker

@ruyadorno
Copy link
Contributor

thanks for the report @MoSattler!

@MohamedLamineAllal
Copy link

MohamedLamineAllal commented Apr 20, 2021

Error: ENOENT: no such file or directory, open error is not fixed '<absolute_current_directory_path>/package.json

Still have the problem in npm v7.7.6

Using npx in a cwd that is a child of the root directory where package.json reside!

"db:migrate": "cd src/Database && npx knex migrate:latest",

This used to work!

Now give the above error! Not the expected behavior! And it's breaking the usual behavior !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Release 7.x work is associated with a specific npm 7 release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants