[BUG] ```npm run / start / test``` cannot find module when path to directory contains ampersand ```&``` or caret ```^```.
Is there an existing issue for this?
- [X] I have searched the existing issues
This issue exists in the latest npm version
- [X] I am using the latest npm
Current Behavior
If script defined in package.json ( npm start / npm test / npm run test ) depends on something installed locally in node_modules directory (react-scripts, jest, etc.), e.g.:
# example package.json file
"scripts": {
"test": "jest", # this script fails
"start": "react-scripts start" # this also
}
and ( important! ) path to current working directory contains ampersand & or caret ^, then npm does not see these dependencies and as a result throws the following error:
PS C:\Users\mateu\Desktop\a&> npm run test
> [email protected] test
> jest
'\node_modules\.bin\' is not recognized as an internal or external command,
operable program or batch file.
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module 'C:\Users\mateu\Desktop\jest\bin\jest.js'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
In the above situation, installing jest globally also did not make the script run.
Similar bug was also incorrectly reported as Next.js problem here: https://github.com/vercel/next.js/issues/26906
Expected Behavior
I expect the behavior (script runs) to be similar to the situation where the path does not contain a special character ( & / ^ ), or I expect npm to inform user about incorrect path of the working directory - just like when the command npm init -y is issued:
PS C:\Users\mateu\Desktop\a&> npm init -y
npm ERR! Invalid name: "a&"
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\mateu\AppData\Local\npm-cache\_logs\2022-01-08T22_38_24_861Z-debug-0.log
Steps To Reproduce
- Create directory with correct name
- Initialize empty package.json file:
npm init -y - Install something which provide console command, for example:
npm install jest - Add new script in package.json with command mentioned above:
# package.json
"scripts": [
"test": "jest",
],
- Change name of directory to "&" or "^"
- Run the defined npm script via
npm run testornpm test- both fail
Environment
- npm: 8.3.0
- Node.js: 17.3.0
- OS Name: Windows 10
- npm config:
; "builtin" config from C:\Program Files\nodejs\node_modules\npm\npmrc
prefix = "C:\\Users\\mateu\\AppData\\Roaming\\npm"
; node bin location = C:\Program Files\nodejs\node.exe
; cwd = C:\Users\mateu
; HOME = C:\Users\mateu
Seeing the same behavior under v8.0.0.
Confirmed. Same issue here in v8.5.2.
Same here, issue still encountered in v8.13.2. Any workaround?
Same here with NPM v9.6.3 on a Node v14.21.3
Same here on npm 10.2.4. STILL no workaround??
the workaround would be to rename your directory.
Thought about adding "Username my organization provided for me has an ampersand in it so I can't rename the directory" but I thought it was extraneous info.
nope, since it's extremely uncommon to have non-alphanumeric chars in a username in any part of computing, that's pretty important info to state explicitly.
Except for when you're on a thread about special characters causing an issue. Also, workaround found: use yarn. Zero problems.
What constitutes a valid pathname is not up for debate. Systems allow names such as R&D, therefore they should be handled properly by libraries. Any path/fs library worth being used should handle spaces and "special" characters properly. If it can be represented on the OS, it should be properly handled by the library.
@Voltra i wasn't debating it. I was just pointing out that a workaround does exist.
I'll try to have a look if I can find a way to fix the paths handling (in npm or node's module resolution directly)
It looks to me like that the issue might come from promise-spawn's path handling on Windows, quite likely this part