cli icon indicating copy to clipboard operation
cli copied to clipboard

[BUG] ```npm run / start / test``` cannot find module when path to directory contains ampersand ```&``` or caret ```^```.

Open Zazzik1 opened this issue 3 years ago • 14 comments

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

log from the above command

Steps To Reproduce

  1. Create directory with correct name
  2. Initialize empty package.json file: npm init -y
  3. Install something which provide console command, for example: npm install jest
  4. Add new script in package.json with command mentioned above:
# package.json
  "scripts": [
    "test": "jest",
  ],
  1. Change name of directory to "&" or "^"
  2. Run the defined npm script via npm run test or npm 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

Zazzik1 avatar Jan 08 '22 22:01 Zazzik1

Seeing the same behavior under v8.0.0.

philipposslicher avatar Feb 15 '22 19:02 philipposslicher

Confirmed. Same issue here in v8.5.2.

PwrSrg avatar Apr 13 '22 15:04 PwrSrg

Same here, issue still encountered in v8.13.2. Any workaround?

kenjipm avatar Jun 30 '22 10:06 kenjipm

Same here with NPM v9.6.3 on a Node v14.21.3

Voltra avatar May 03 '23 12:05 Voltra

Same here on npm 10.2.4. STILL no workaround??

ericburrell-23-1 avatar Mar 04 '24 21:03 ericburrell-23-1

the workaround would be to rename your directory.

ljharb avatar Mar 04 '24 21:03 ljharb

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.

ericburrell-23-1 avatar Mar 04 '24 21:03 ericburrell-23-1

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.

ljharb avatar Mar 04 '24 21:03 ljharb

Except for when you're on a thread about special characters causing an issue. Also, workaround found: use yarn. Zero problems.

ericburrell-23-1 avatar Mar 04 '24 21:03 ericburrell-23-1

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 avatar Mar 04 '24 22:03 Voltra

@Voltra i wasn't debating it. I was just pointing out that a workaround does exist.

ljharb avatar Mar 04 '24 22:03 ljharb

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)

Voltra avatar Mar 05 '24 14:03 Voltra

It looks to me like that the issue might come from promise-spawn's path handling on Windows, quite likely this part

Voltra avatar Mar 05 '24 15:03 Voltra