Problems installing Node and NPM via Homebrew

homebrewpermissionterminal

As of Monday morning, running the nodemon and npm run dev commands for a project I'm working on failed, and I traced it to some error with NPM itself.

macbookpro@MacBookPro:/usr/local/lib$ npm -v
internal/modules/cjs/loader.js:983
throw err;
^
Error: Cannot find module '../lib/utils/unsupported.js'
Require stack:
- /usr/local/lib/node_modules/npm/bin/npm-cli.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15)
at Function.Module._load (internal/modules/cjs/loader.js:862:27)
at Module.require (internal/modules/cjs/loader.js:1040:19)
at require (internal/modules/cjs/helpers.js:72: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:153:3)
at Module._compile (internal/modules/cjs/loader.js:1151:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/usr/local/lib/node_modules/npm/bin/npm-cli.js' ]
}
macbookpro@MacBookPro:/usr/local/lib$ node -v
v13.8.0
macbookpro@MacBookPro:/usr/local/lib$ ls -al node_modules/npm/bin/
total 56
drwxr-xr-x 9 macbookpro staff 288B 24 Feb 09:03 ./
drwxr-xr-x 25 root wheel 800B 7 Aug 2019 ../
drwxr-xr-x 4 macbookpro staff 128B 24 Feb 09:03 node-gyp-bin/
-rwxr-xr-x 1 macbookpro staff 893B 24 Feb 09:03 npm*
-rwxr-xr-x 1 macbookpro staff 4.5K 24 Feb 09:03 npm-cli.js*
-rw-r--r-- 1 macbookpro staff 483B 24 Feb 09:03 npm.cmd
-rw-r--r-- 1 macbookpro staff 887B 24 Feb 09:03 npx
-rwxr-xr-x 1 macbookpro staff 177B 24 Feb 09:03 npx-cli.js*
-rw-r--r-- 1 macbookpro staff 539B 24 Feb 09:03 npx.cmd
macbookpro@MacBookPro:/usr/local/lib$

So while Node itself was running, NPM wasn't, in spite of the fact that its file was present.

In development I use nodemon and npm run dev in two Terminal tabs — both were running Sunday night, before stopping them and then sleeping the Mac.

I was wondering if a permission had been altered.

I tried Homebrew, which installed Node but not NPM, so I had to remove that.

I tried the official Node installer for Mac, but it's an old version that caused problems, so I had to remove that.

I went down the nuclear path, and removed Node wherever it could be found:

brew uninstall node;
which node;
sudo rm -rf /usr/local/bin/node;
sudo rm -rf /usr/local/lib/node_modules/npm/
brew doctor;
brew cleanup --prune-prefix

I then tried the official instruction to install Node via the command line:

curl "https://nodejs.org/dist/latest/node-${VERSION:-$(wget -qO- https://nodejs.org/dist/latest/ | sed -nE 's|.*>node-(.*)\.pkg</a>.*|\1|p')}.pkg" > "$HOME/Downloads/node-latest.pkg" && sudo installer -store -pkg "$HOME/Downloads/node-latest.pkg" -target "/"

… which failed:

-bash: wget: command not found

… and when I attempted to install wget:

brew install wget

… I got more errors:

Error: The brew link step did not complete successfully The formula
built, but is not symlinked into /usr/local Could not symlink
share/locale/cs/LC_MESSAGES/libidn2.mo
/usr/local/share/locale/cs/LC_MESSAGES is not writable.

You can try again using: brew link libidn2

… and:

Error: The brew link step did not complete successfully The formula
built, but is not symlinked into /usr/local Could not symlink
share/locale/bg/LC_MESSAGES/wget.mo
/usr/local/share/locale/bg/LC_MESSAGES is not writable.

You can try again using: brew link wget

I had similar problems with Homebrew before the weekend, and I think this is a continuation of that.

When I run brew doctor I get:

Unexpected header files:
  /usr/local/include/node/...

… and there are hundreds of files associated with Node, in spite of having uninstalled it.

I've since found a few different approaches to fix the possible permissions issue:

sudo chown -Rwhoami:admin /usr/local/

… and:

sudo chown -R $(whoami) $(brew --prefix)/*

… but since I have no idea what the possible consequences of running either of these would be, some expert advice would be much welcome!

Best Answer

I just solved this same error. What worked for me was:

brew install node
sudo chmod 776 /usr/local/lib
brew link --overwrite node
sudo chmod 755 /usr/local/lib

When I ran npm -v I got the same error:

internal/modules/cjs/loader.js:983
  throw err;

To solve it I ran brew reinstall node and everything is running as expected now.