-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[BUG] npm on windows can't run scripts with ".", relative paths "../" #2880
Comments
Can confirm: Replacing with backslash fixes the issue: Steps To Reproduce:A command like this run in windows powershell. Gives error '.' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code 1 Working-Example:
Environment:npm: 7.6.3 |
In that example, you don't need to hardcode node_modules at all - you can do Hardcoding node_modules should be rare. |
IIUC npm uses cmd.exe as its default shell to execute scripts on Windows, which has this limitation. Another workaround is to surround the path in quotes, e.g. |
Surrounding with quotes didn't work for me when I tried. The fact that this is supported by yarn may be due to a different shell being used? |
Hmm, it works in plain cmd.exe but I didn't try through npm, sorry. I guess npm does some processing on it which might be getting in the way, like with #2731 (the fix for that issue was merged into run-script but npm v7.6.3 doesn't have it yet). |
You are right about the use case but that is not the point. I can also reproduce the error when trying to run a cmd script from npm. And they did work on npm version 6.4.1 |
this is a difficult one because your expected behavior statement "the string should be converted to make it cross platform" means that we would have to parse bash scripts and port them to cmd scripts and vice versa. this is an absolutely massive undertaking filled with a huge number of edge cases and gotchas. some versions of npm 7 attempted a form of this, but caused a lot of failures so we removed that work in favor of just running the string as-is. for most use cases this will do the right thing and is far more intuitive to write and debug. as @ljharb mentioned, you should be able to simply run it is, however, strange that running it in a command prompt directly works while running it through an npm script does not. i'll take a look and see if i can reproduce that and what might be happening. |
Interestingly, it works if I wrap the entire command in yet another set of quotes. If the following works from a cmd terminal: |
is that on the latest npm? 7.8.0? |
ok, so to break this down. if you have a script defined like: { "scripts": { "test": "./node_modules/.bin/foo" } } what we run is if you copy that to a running shell and run it, it fails with the if you wrap it in quotes once, like { "scripts": { "test": "\"./node_modules/.bin/foo\"" } } then we now run
since we do pass putting a second pair of quotes gets around this because the inner quotes will be left alone while the outer quotes are removed. the problem lies in the handling of arguments. we can't just wrap the whole string in two pairs of quotes, because then the shell will think that the entire string is a command, which will give you more not recognized as a command errors. theoretically, we would have to parse the string and look for the first space and wrap everything up to that in a pair of quotes, then append the arguments to that and wrap the whole thing in another pair of quotes. the tricky part of that is the numerous ways that a space could exist and still be part of the first parameter (i.e. the "command"), it could be wrapped in quotes, it could be a literal space, it could be a the challenges and edge cases here are why we opted to simply run the string we're given and not attempt conversion. as an added gotcha, shell builtins don't work if you quote them. i.e. |
Closing. |
Why is this closed? This worked in older npm versions and is still broken now in 8.1.1. I can't imagine anyone is happy with the double quoting "solution" as suggested, since it makes npm scripts more unreadable and is a lot more error-prone. |
Current Behavior:
On windows npm cannot run scripts like
"build": "../node_modules/.bin/tsc",
Error:
'..' is not recognized as an internal or external command,
operable program or batch file.
Replacing with ..\ makes it work.
Expected Behavior:
The string should be converted to make it cross-platform.
As a note, it does work with Yarn.
Steps To Reproduce:
Place in script task something with "../" or "./" on WINDOWS.
Environment:
npm: '7.6.2',
node: '14.4.0',
The text was updated successfully, but these errors were encountered: