30

I just came back to work on a React project after working in the Rails environment and found that I cannot run any NPM commands in my Mac terminal. I've tried all the remedies that I have come across but none have worked. The bigger issue is what caused this and how can I avoid it again.

Here is the error. What do I need to do? Please help.

$npm install            

internal/modules/cjs/loader.js:550
    throw err;
    ^

Error: Cannot find module '../lib/utils/unsupported.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:548:15)
    at Function.Module._load (internal/modules/cjs/loader.js:475:25)
    at Module.require (internal/modules/cjs/loader.js:598:17)
    at require (internal/modules/cjs/helpers.js:11:18)
    at /usr/local/lib/node_modules/npm/bin/npm-cli.js:19:21
    at Object.<anonymous> (/usr/local/lib/node_modules/npm/bin/npm-cli.js:92:3)
    at Module._compile (internal/modules/cjs/loader.js:654:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
    at Module.load (internal/modules/cjs/loader.js:566:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:506:12)

I have tried the following:

$brew update
$brew uninstall node
$brew install node

After which the following commands come up as so when $which npm was unknown command before:

$which npm
/usr/local/bin/npm

$which node
/usr/local/bin/node

I even tried running:

/usr/local/bin/npm -v

but get the same error as above while

/usr/local/bin/node -v
v9.11.1

works fine.

added the following to my .zshrc file:

export PATH=/usr/local/bin/node:$PATH
export PATH=/usr/local/bin/npm:$PATH

I have also tried:

$brew uninstall --force node
$brew link --overwrite node 
Warning: Already linked: /usr/local/Cellar/node/9.11.1
To relink: brew unlink node && brew link node

$rm -rf /usr/local/lib/node_modules/npm    

then,

$node install

but that does not work, either.

$echo $NODE_PATH

does come up empty, so I may need to added the following to my .bashrc.

export PATH="/usr/local/bin/npm:/usr/local/bin/node:/usr/local/bin:$PATH" 

That does not work either.

bigmugcup
  • 851
  • 2
  • 10
  • 20
  • do you have `require('../lib/utils/unsupported.js')` somewhere in your code? – Bryan Chen Apr 06 '18 at 05:13
  • No, I don't have any reference to unsupported.js. NPM commands don't work on any project or directory. – bigmugcup Apr 06 '18 at 05:22
  • Looks like I was able to fix the issue by downloading and installing Node from https://nodejs.org. Did $brew uninstall --ignore-dependencies node, then downloaded and installed from nodejs.org. I have no idea why the brew installation of Node and NPM stopped working. If anyone has any idea, please let me know. – bigmugcup Apr 06 '18 at 05:41
  • Related issue: https://github.com/nodejs/node/issues/19997 – k06a Apr 13 '18 at 06:23
  • 1
    None of below-written answers fixed my problem. – Zeth Dec 28 '18 at 09:18

12 Answers12

49
  1. Uninstall node brew uninstall --force node
  2. Remove the /usr/local/lib/node_modules folder
  3. Install it again brew install node
Lucas Lopes
  • 806
  • 7
  • 5
  • 6
    same for me BUT I had to get rid of the modules files present under "/usr/local/lib/node_modules/" before doing the reinstall. – Orsius Feb 06 '19 at 09:50
  • 2
    worked for me as well but only after deleting `/usr/local/lib/node_modules` – yevg Nov 05 '19 at 00:41
  • 1
    Same here, only worked after removing the`/usr/local/lib/node_modules` folder. @Lucas Lopes, maybe update your answer to add that? – Ioanna Nov 19 '19 at 05:58
  • 1
    i had to remove `/usr/local/lib/node_modules/npm` only and not the whole folder `/usr/local/lib/node_modules` – Zorox Feb 21 '20 at 23:25
  • 2
    First should install the brew command : sudo apt install linuxbrew-wrapper After run the 1. uninstall node command then remove the folder using this command : sudo rm -r /usr/local/lib/node_modules. finally run the 3 and press Enter – Dilshan Dilip Feb 23 '20 at 17:55
  • 1
    wow this worked, ty – Jeremy Bader Dec 17 '20 at 06:25
10

Assume this happens on MacOS, below solution works for me.

  1. brew uninstall --force node
  2. Visit https://nodejs.org/en/ and download the LTS version and install, the error was gone
Popeye
  • 1,444
  • 17
  • 14
7

This happened to me after installing yarn.

in a terminal:

brew uninstall --force --ignore-dependencies node
brew install node

if you have trouble symlinking node, in the same terminal:

sudo chown -R `whoami`:admin /usr/local/include
sudo chown -R `whoami`:admin /usr/local/bin
sudo chown -R `whoami`:admin /usr/local/share
sudo chown -R `whoami`:admin /usr/local/lib
brew link --overwrite node

Check that your re-installation worked by typing the following in the terminal:

node -v
T. Dayya
  • 379
  • 5
  • 8
4

I had an issue after upgrade my mac. I tried different solutions available on stack overflow and nothing work for me.

This solution works for me.

  1. Uninstall node all version brew uninstall --force node
  2. Install it again by this brew install node

First point remove all node versions if available.

Done. Enjoy coding.

Rehan Ali
  • 322
  • 2
  • 8
3

Try this one: https://gist.github.com/DanHerbert/9520689

"If you're a Homebrew user and you installed node via Homebrew, there is a major philosophical issue with the way Homebrew and NPM work together."

Kudos to Dan Herbert for the solution.

imazio
  • 31
  • 2
3

Remove all file from following folder

A)C:\Users\\AppData\Roaming\npm

B)C:\Users\\AppData\Roaming\npm-cache

and install node again

then execute command "npm install -g @angular/cli"

Manoj Gupta
  • 367
  • 3
  • 7
3

None of the other answered solved my issue. After reinstalling Node, I still got the same error (even when I just wrote npm -v).

I followed this answer here (removing everything called node and npm), which solved it for me.

Zeth
  • 1,401
  • 1
  • 25
  • 54
3

In my case, the solution to the problem was reinstalling the node using the official installer from https://nodejs.org (Mac OS).

Solution:

  1. Download node installer from https://nodejs.org
  2. Install it

P.S. The problem appeared after yarn installation.

bhoodream
  • 329
  • 2
  • 9
1

One other suggestion after trying these that finally worked for me was simply upgrading my node by downloading the latest via the download and package installer from here:

https://nodejs.org/en/download/

Before doing this trying to run any npm command gave me the same error brought in OP's question

npm commands started working for me after that.

0

I'm sorry, but in my case I get the same error, because, my index.js is inside de src folder, make sure that you are in the seme directory of your index.js to run

Higor Tavares
  • 63
  • 2
  • 9
0

In my case, this happened after I performed a machine-wide clean-up of all node_modules folders. I had node installed with nvm and I started getting this error, no matter what node-related action I wanted to trigger:

Error: Cannot find module '../lib/utils/unsupported.js'

I solved it by doing the following:

  1. Restarted my computer
  2. Uninstalled nvm
  3. Installed a different node version
Paul Razvan Berg
  • 6,292
  • 2
  • 33
  • 54
-2

I had the same error after brew upgradeon mac. I did brew postinstall node and that fixed it.