zkat / npx

execute npm package binaries (moved)
https://github.com/npm/npx
Other
2.63k stars 105 forks source link

somethig wrong with npx Command failed: C:\“Program Files”\nodejs\node.exe #110

Closed bluelovers closed 6 years ago

bluelovers commented 7 years ago

my npx always show this error

Command failed: C:\"Program Files"\nodejs\node.exe C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js config get cache --parseable module.js:487

20170814-082755_tavgg 20170814-082758_dyim1

Pomax commented 7 years ago

Just ran into this, too. Looks like npx forgets to properly quote paths. Which is in itself a bit odd because no path issues should be necessary, just calling node without any absolute path as process command should auto-resolve since it adds itself to the PATH during Windows installation, and running npm is a matter of issuing npm.cmd rather than npm on the win32 platform (which is a misnomer and simply what Node calls windows architectures)

rossipedia commented 7 years ago

Running into the same thing here:

λ npx --help
Command failed: C:\"Program Files"\nodejs\node.exe C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js config get cache --parseable
module.js:491
    throw err;
    ^

Error: Cannot find module 'C:\Program'
Pomax commented 7 years ago

Might be worth bisecting this against previous versions of npx, since this seems to be a recent issue.

zkat commented 7 years ago

I'm pretty sure this is happening because different Windows shells require different types of path escapes -- on Windows, npx uses CMD-style escapes, for example.

Pomax commented 7 years ago

I use cmd exclusively, given that powershell does not support "standard" shell syntax (like && to chain commands)

sylvainlap commented 7 years ago

Got the same issue, with Git Bash on Win10.

rossipedia commented 7 years ago

This issue should be fixed in the latest npx (at least it was for me), which is not included in the latest nodejs installer. You can manually update/install npx globally (npm install -g npx@latest) to get the fix

Pomax commented 7 years ago

@rossipedia you mean it's fixed on master, rather than 9.6.0? (there is no tag newer than that, and that's the latest version npm knows about)

rossipedia commented 7 years ago

Hmmmm npx@latest fixed it for me. There may still be an issue then.

noelleleigh commented 7 years ago

Also still having this issue on npx@latest on Windows 10 Powershell. Here's a brief interaction I had with @npm_support on it a few versions back: https://twitter.com/noahdleigh/status/890355025104556032

Behavior hasn't changed on Node 8.4.0, npm 5.4.1, npx 9.6.0

rossipedia commented 7 years ago

When I looked into it, it seems as though path quoting is performed incorrectly on Windows. Instead of quoting each path element (eg: C:\"Program Files"\nodejs), if there are any spaces the entire path needs to be quoted (ie: "C:\Program Files\nodejs").

michaeljota commented 7 years ago

@noahleigh If you are using Node 8.4.0 or newer, it falls to installed npx, which is npx 9.2.1.

I just deleted the npx files in the node folder for this to work.

zkat commented 7 years ago

tbh I probably need help here. I've spent time messing around with different escape methods, and they seem to vary by situation :\

michaeljota commented 7 years ago

@zkat latest version seems to run fine. Did you find bugs in Windows? Or with the fix for Windows something else got broken?

rossipedia commented 7 years ago

@zkat I'll try my hand at a PR here this weekend, I think I remember where the problematic method is

noelleleigh commented 7 years ago

@michaeljota I deleted npx and npx.cmd from C:\Program Files\nodejs and now it's using the updated 9.6.0 instead of 9.2.1!

PS C:\Users\noahleigh> npx -v
9.6.0
PS C:\Users\noahleigh> npx cowsay hey
npx: installed 9 in 3.627s
C:\Users\noahleigh\AppData\Roaming\npm-cache\_npx\4696\node_modules\cowsay\cli.js
 _____
< hey >
 -----
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Why would node fail to use the updated version even after npm install npx@latest -g?

michaeljota commented 7 years ago

Because, as it does when updating npm in that way, Node will always lookup first in the node folder, and then in the modules folder. 😄. At least, in Windows it works that way.

rossipedia commented 7 years ago

A bit more on that:

When nodejs is installed, the installation folder (eg: C:\Program Files\nodejs) is added to the system %PATH% environment variable.

However, your npm installation folder's bin directory %APPDATA%\bin is added to your user's %PATH% environment variable (since %APPDATAT% is essentially just %USERPROFILE%\AppData\Roaming).

Now here's the part where it breaks: in Windows, to get the final %PATH% value, the user's variable is appended to the system variable, which means that the nodejs installation folder comes before the npm bin folder, whiiiiiich means that the installed np[mx] will always be given preference over the one installed via npm install --global.

I've personally fixed it by removing %PROGRAMFILES%\nodejs from my system path, and adding it to my user path after the %APPDATA%\npm entry.

noelleleigh commented 7 years ago

@rossipedia Why does the nodejs installer put npm stuff in the Program Files folder if the "real" and updated versions live in App Data? Why not just keep the executables in there exclusively?

michaeljota commented 7 years ago

I think it does now because of backwards compatibility only. It has been broken since day one. The thing is that if the instalation append the app data folder route before the program route it would be fine.

El sept. 16, 2017 6:12, "Noah Leigh" notifications@github.com escribió:

@rossipedia https://github.com/rossipedia Why does the nodejs installer put npm stuff in the Program Files folder if the "real" and updated versions live in App Data? Why not just keep the executables in there exclusively?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/zkat/npx/issues/110#issuecomment-329959471, or mute the thread https://github.com/notifications/unsubscribe-auth/AKBWAAr8CRz2WzsvAdOXesD8wSx8hsW6ks5si58KgaJpZM4O11_x .

noelleleigh commented 7 years ago

Relevant nodejs issue: https://github.com/nodejs/node/issues/15095

duluca commented 7 years ago

@noahleigh workaround to manually clean out 9.2.1 files worked for me. Curiously (and to my surprise) running cd C:\"Program Files"\nodejs\ in PowerShell works as expected, meaning it actually changes cwd to that folder. Per @rossipedia point, I think npx should use whatever version of npm is globally installed. I think it's an important UX decision.

kim3er commented 7 years ago

Deleting the files from Program Files, as @noahleigh suggested, and updating to 9.6.0 resolved the issue for me.

CallMeLaNN commented 7 years ago

I think it is good follow npm way to refer the current version for consistency since npx shipped with node installer just like npm. So npx in C:\Program Files\nodejs will 'redirect' to npx in %USERPROFILE%\AppData\Roaming\npm. Otherwise it will be harder for Windows user to update npx for newly installed node.

Deleting the files from Program Files, as @noahleigh suggested, and updating to 9.6.0 resolved the issue for me also.

michaeljota commented 7 years ago

npm does not refer the current version in the way you describe. In deed, it refers to the current version the way npx install in nodejs folder does. That's why tools like npm-windows-upgrade exists.

kedrzu commented 7 years ago

@rossipedia your solution works perfectly

boxfoot commented 7 years ago

I'm getting a different error that also seems to be related to Windows path escaping. I'm running up-to-date npm and npx.

I'm also confused b/c it seems to be reporting two errors - a failure to mkdirp, and then the error handler seems unable to load npm.config...

image

Is this the same (or related?)

mmueller013 commented 7 years ago

I got a similar error in an npm script and fixed by using the 8.3 ntfs filesystem shortname. You can retrieve the shortname by "dir /x" PROGRA~1

e.g.: "c:\PROGRA~1\7-Zip\7z.exe"

zkat commented 6 years ago

This is a duplicate of #100 and should be resolved by #181