npm or yarn command error – cli.js not found

While working on a project, I needed to use npm packages and I received a strange error while trying to use them. I ended up with the same error for yarn commands. Now, I had used npm some time back and did not have any issues.

Below is the error I ended up with each time I tried a npm or a yarn command.

internal/modules/cjs/loader.js:883  

throw err;

Error: Cannot find module ‘C:\Program Files\nodejs\node_modules\npm\bin\node_modules\npm\bin\npm-cli.js’

Solutions I tried and did not work for me:

  1. Reinstall npm.
  2. Repair npm
  3. Update my .npmrc file
  4. Restart the system! – that’s what the IT service center asks you to do first thing when you contact them….

What solved the problem?

So, obviously I tried and find the file mentioned in the error in my node.js folder. I obviously found it and banged my head on the wall!

Finally, I tried to trace the file by exact path when I noticed that the following part was extra in there –

node_modules\npm\bin

The correct path should have been “C:\Program Files\nodejs\node_modules\npm\bin\ npm-cli.js”

And that’s wen I went back and looked at my environment variables and found following culprit!!

npm path environment entry

All I had to do was delete this entry, restart my command line and BINGO!

Finally, the moral of the story is – the system variable for npm should be as below:

npm path environment entry

Now, The question is how did that erroneous entry landed in my system variables? There could be multiple reasons for it like:

  1. I manually added it and obviously do not remember!
  2. At some point of time, my system some how had multiple versions of node.js
  3. It got corrupted while updating the node.js versions.

Hope this helps someone in need. Till then,

Happy Coding 😊