Skip to content
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] npm run / start / test cannot find module when path to directory contains ampersand & or caret ^. #4225

Open
2 tasks done
Zazzik1 opened this issue Jan 8, 2022 · 13 comments
Labels
Bug thing that needs fixing Priority 2 secondary priority issue Release 8.x work is associated with a specific npm 8 release

Comments

@Zazzik1
Copy link

Zazzik1 commented Jan 8, 2022

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • 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

> a@1.0.0 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: vercel/next.js#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 Zazzik1 added Bug thing that needs fixing Needs Triage needs review for next steps Release 8.x work is associated with a specific npm 8 release labels Jan 8, 2022
@philipposslicher
Copy link

Seeing the same behavior under v8.0.0.

@wraithgar wraithgar added Priority 2 secondary priority issue and removed Needs Triage needs review for next steps labels Mar 14, 2022
@PwrSrg
Copy link

PwrSrg commented Apr 13, 2022

Confirmed. Same issue here in v8.5.2.

@kenjipm
Copy link

kenjipm commented Jun 30, 2022

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

@Voltra
Copy link

Voltra commented May 3, 2023

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

@ericburrell-23-1
Copy link

Same here on npm 10.2.4. STILL no workaround??

@ljharb
Copy link
Collaborator

ljharb commented Mar 4, 2024

the workaround would be to rename your directory.

@ericburrell-23-1
Copy link

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.

@ljharb
Copy link
Collaborator

ljharb commented Mar 4, 2024

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.

@ericburrell-23-1
Copy link

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

@Voltra
Copy link

Voltra commented Mar 4, 2024

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.

@ljharb
Copy link
Collaborator

ljharb commented Mar 4, 2024

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

@Voltra
Copy link

Voltra commented Mar 5, 2024

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
Copy link

Voltra commented Mar 5, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Priority 2 secondary priority issue Release 8.x work is associated with a specific npm 8 release
Projects
None yet
Development

No branches or pull requests

8 participants