-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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] inconsistent interpretation of backslash passed to npm scripts in windows #2638
Comments
I hit a similar issue: In the following repository: https://github.com/wandyezj/package on windows in cmd.exe using npm version 6.14.9 the following script works which is keyed to: upon upgrading to npm version 7.5.4 via running now errors with: |
(while that should work, in a run-script it should just be keyed to “prettier” - hardcoding node_modules anywhere is a code smell) |
There are some significant benefits to the above approach:
Note: the above issue is caused due to a global NPM version that varies across machines, precisely the problem the above approach avoids. NPM is somewhat of an exception since it needs to be globally installed so it can install other packages. I could create a npm.cmd / npm.sh script that references a specific NPM version in the node_modules and falls back to the global install if npm is not present, but this seems like something that should be supported by default instead of something that needs to be hacked to work around. What is your recommendation of how to achieve the above scenario where I want to version and run tools with the package and have these scripts present in the package scripts instead of relying on global installs of those tools? How would you recommend I adjust my script to achieve the above scenario? Would it be possible to add a unit test to verify that the above functionality continues to work? This significantly disrupted an existing workflow and cost me several hours to debug. Perhaps I am misunderstanding, does npm search first in the node_modules/.bin for scripts to execute instead of the global path? (I'm not sure that would be a good practice from a security perspective as a module could overwrite common commands...) |
npx will always use the local prettier if it’s installed, so i don’t think you need to hardcore the path at all. |
I removed the absolute relative path I tested with However, if a local version is not installed and a global version is installed I'd like to avoid the possibility of calling a globally installed tool entirely, for the reasons mentioned above. I know this seems like nitpicking, but it's a real issue when working across multiple developers' environments (It would be awesome for everyone to set up local machine development environments the same way, but realistically that's not going to happen (The entire reason for the above thread is because different developers had different npm versions installed)). Is there a way to ensure that only the version in the local node_modules/.bin is called? |
In that case no, hardcoding the path is the only way to do that afaik, but then i think you might lose platform path portability (since non-WSL windows uses the bin with “.cmd” on the end) |
Ahh... Thank you for the clarification @ljharb, I suppose I will have to wait for this issue to be resolved in the latest version of NPM, before upgrading. note: This works fine on Windows as cmd.exe has a precedence list for executing items in the path without the extension's presence by looking for the command without the extension (i.e. |
The escaping that npm does was greatly fixed in npm/run-script#22, and we have another effort in place to be more consistent across the cli in parsing and escaping things when we spawn scripts, that effort is happening in the new https://github.com/npm/exec repo. I believe the first PR linked fixed the issue described here. If this is still an issue please feel free to reopen. |
I'm sorry to report a minor issue.
Tools built on node tend to use npm scripts in package.json as shortcuts of various tasks.
Passing paths of files or directories to such tools is a common use case,
but it sometimes meets a difficulty when the path contains backslashes in windows.
Current Behavior:
package.json
index.js
npm start commands:
in npm 7.5.2
in npm 6.4.11
Expected Behavior:
I hope the result is the same as that of corresponding node commands:
in both npm 7.5.2 and npm 6.4.11
but it looks quite hard to resolve.
Steps To Reproduce:
see Current Behavior section.
Environment:
The text was updated successfully, but these errors were encountered: