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 commands failing due to parsing bug #2410

Closed
Chriscbr opened this issue Dec 24, 2020 · 4 comments · Fixed by #2448
Closed

[BUG] npx commands failing due to parsing bug #2410

Chriscbr opened this issue Dec 24, 2020 · 4 comments · Fixed by #2448
Assignees
Labels
Bug thing that needs fixing Release 7.x work is associated with a specific npm 7 release

Comments

@Chriscbr
Copy link

Chriscbr commented Dec 24, 2020

Current Behavior:

$ npx node -e "console.log(3)"
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `node -e console.log(3)'
npm ERR! code 2
npm ERR! path /Users/rybickic/Developer/empty-dir
npm ERR! command failed
npm ERR! command sh -c node -e console.log(3)

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/rybickic/.npm/_logs/2020-12-24T08_25_37_431Z-debug.log

Expected Behavior:

$ npx node -e "console.log(3)"
3

Steps To Reproduce:

See commands above.

I've tried running this with several npm version combinations, and it appears to be a regression introduced in npm 7.1.0. That is, with the npm@7.0.15 the command succeeds (prints 3), but starting in npm@7.1.0 it fails. This was brought to my attention when I was updating to the latest node release (v15.5.0) two days ago via nvm, which updates npm to 7.3.0 (changelog).

Environment:

  • OS: macOS Catalina 10.15.7
  • Node: 15.5.0
  • npm: 7.3.0
@Chriscbr Chriscbr 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 Dec 24, 2020
@Chriscbr Chriscbr changed the title [BUG] npx commands failing [BUG] npx commands failing due to parsing bug Dec 24, 2020
@ljharb
Copy link
Collaborator

ljharb commented Dec 24, 2020

Why would you need an npx prefix to run node?

@Chriscbr
Copy link
Author

We were using it so that in a script, we could change the PATH environment variable to include the project's node_modules, and run commands from packages inside, e.g.:

PATH="$(npx node -e "console.log(process.env.PATH)")" some-command

My understanding is npx modifies the context a process runs in by automatically determining the right paths to add. If you omit the npx prefix, and ran node -e "console.log(process.env.PATH)" instead, the output value will not contain the project's node_modules, so some-command will fail (not be found).

We were using npx -c "echo $PATH" previously, but discovered this fails on Windows so we switched to the code above. For now, npx -c 'node -e "console.log(process.env.PATH)"' is another temporary solution we've found.

For the tool I am building, we don't want consumers to have to manually prefix commands with "npx" depending on whether it's an npm script or a python script etc. so that's why we are trying to set up the environment like this.

(This stackoverflow post is also in the same spirit of what I'm describing / the challenges - https://stackoverflow.com/questions/39990788/achieve-npm-run-x-behavior-without-a-scripts-entry)

@nlf nlf removed the Needs Triage needs review for next steps label Jan 7, 2021
@nlf nlf self-assigned this Jan 7, 2021
@Chriscbr
Copy link
Author

I tried this on 7.4.0 and the example I gave works now. 👍

@ljharb
Copy link
Collaborator

ljharb commented Jan 19, 2021

Fixed by #2448.

@ljharb ljharb closed this as completed Jan 19, 2021
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.

3 participants