npm / npm/cli

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

Open
#4,225 14 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Priority 2 Release 8.x
Dominant language
JavaScript
Stars
10.1k
Forks
4.7k
Avg merge
2d 2h
Merged PRs (30d)
19

Description

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: 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the npm run, npm test, or npm start failure on Windows with a working-directory path containing & or ^, using the package.json scripts and versions described. Trace npm's local command resolution and script execution for these paths. Done means local dependencies run successfully from such directories, or npm reports a clear invalid-path error.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
cli, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.