NPM is not working on windows - double c drive in path

Viewed 375

I've installed node version 14.16.0 (according to an online course that I'm taking). I've tried to install angular with npm using the command npm install -g @angular/cli@11.2.3 (again according to the course). It threw an error so I ran npm --version to see if npm is working at all, and I got the same error.

Please notice that the path seems weird (C: drive is written twice).

This is the error:

$ npm --version
internal/modules/cjs/loader.js:883
  throw err;
  ^

Error: Cannot find module 'C:\c\Program Files\nodejs\node_modules\npm\bin\npm-cli.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)

    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72
:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
internal/modules/cjs/loader.js:883
  throw err;
  ^

Error: Cannot find module 'C:\c\Program Files\nodejs\node_modules\npm\bin\npm-cli.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)

    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72
:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

2 Answers

If someone faces the same issue - it was solved just by uninstalling node and installing the latest version. I guess there's something wrong with this specific version on windows.

EDIT:

after figuring out I had to use a specific version of node (14 in my case) because of other dependencies, I went deeper, and found that in the \c\Program Files\nodejs\npm file, there's an addition of the path variable, and then sending it to node.exe. The simple solution I found was to remove the \c prefix that was added to my path (in two places inside the script), and it worked! NPM_CLI_JS=${NPM_CLI_JS#"/c"}

This issue can be caused by many weird ways, one of them is nesting your project in a very deep directory.

For example:

C:\users\user\documents\webDevelopment\javascriptProjects\**project1**

nesting your project in such way apparently makes npm not able to recognize all your packages on windows.

Edit: After Further investigation, it looks like my issue occurred because one of the parent directories contained an ampersand (&).

Related