Problems with updating npm and Node.js (incompatibility between `npm install -g npm` and homebrew installation)

Hi everyone, I’m a neophyte and I’m learning to use node and npm.
In this regard, this morning I was initializing a folder npm init when a warning appeared that a new version was available.
screen

So I used the command: sudo npm install -g npm.
Before doing this, the version of npm on my Mac was 6.9.0 and the version of node was 8.9.4.

After the update the version has become 6.10.0 but has stopped working. Running any command such as npm init, ornpm install stylelint the command line returned the error:

MacBook-Pro-username:try username$ npm install stylelint
npm ERR! code MODULE_NOT_FOUND
npm ERR! Cannot find module './any.js'

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/username/.npm/_logs/2019-07-08T11_07_07_324Z-debug.log

Then, failing to figure out how to update the node I launched the brew install node command and it was an error because I probably didn’t install it via Homebrew (it’s been too long and I didn’t remember).

Now if I run the command node --version it still gives me the old version, so it’s possible that now I have two versions of node. When I run the npm init command, the error message has changed:

module.js:540
    throw err;
    ^

Error: Cannot find module 'semver'
    at Function.Module._resolveFilename (module.js:538:15)
    at Function.Module._load (module.js:468:25)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/unsupported.js:2:14)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)

I know I’ve made a mess. I really don’t know how to get out of it so I thank you in advance for the help.

It looks like a broken install of npm. Reinstalling npm should hopefully fix it. You could reinstall using the node installer from nodejs (which includes a version of npm), or using homebrew.

homebrew and the nodejs installers both install to /usr/local so you probably only have one copy. You can check with:

which -a npm
which -a node

First of all, thanks for the answer @shadowspawn I did as you suggested and in fact there seems to be only one version for both:
which -a npm > /usr/local/bin/npm
which -a node > /usr/local/bin/node

In the post I suggested two different nodejs installations because if I try to do a nodejs update via Homebrew:

brew upgrade node

I get the following message:

Error: node 12.5.0 already installed

while if I start the command:

node -v

tells me that the installed version is v8.9.4.

However I’m not an expert user so tell me what you suggest me to do now to better understand the problem? If there are no other investigations we can do, and the only way is to reinstall nodejs and npm, I’d prefer a method that uses Homebrew to reinstall everything.
Waiting for your reply, I thank you.

I expect brew knows you told it to install node 12.5.0, but in fact it has been overwritten by installing another way.

When you run brew doctor, does it finish with a warning about node? I tried recreating the state you might be in and I see this:

Warning: You have unlinked kegs in your Cellar.
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
  node

And running that command fails and gives some tips about how to fix it:

$ brew upgrade node
Error: node 12.5.0 already installed
$ node --version
v10.16.0
$ brew link node
Linking /usr/local/Cellar/node/12.5.0... 
Error: Could not symlink bin/node
Target /usr/local/bin/node
already exists. You may want to remove it:
  rm '/usr/local/bin/node'

To force the link and overwrite all conflicting files:
  brew link --overwrite node

To list all files that would be deleted:
  brew link --overwrite --dry-run node

Hi @shadowspawn,
everything you wrote was verified, and I tried to follow all the steps, but I’m not sure of the result.
I’ll list in an orderly way everything I did and the message I got:

  1. brew doctor
    which partially gave me the message you had assumed along with other information:
Warning: You have unlinked kegs in your Cellar.
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
  node

here two screens with almost complete message (unfortunately I forgot to complete the content):

  1. brew upgrade node
    who returned the following message to me:
    Error: node 12.5.0 already installed

  2. node -v
    who returned the following message to me:
    v8.9.4

  3. brew link node
    who returned the following message to me:

Linking /usr/local/Cellar/node/12.5.0... 
Error: Could not symlink bin/node
Target /usr/local/bin/node
already exists. You may want to remove it:
  rm '/usr/local/bin/node'

To force the link and overwrite all conflicting files:
  brew link --overwrite node

To list all files that would be deleted:
  brew link --overwrite --dry-run node
  1. from this point on I didn’t understand how it was better to act, so first I tried to launch the command brew link --overwrite node who returned the following message to me:
Linking /usr/local/Cellar/node/12.5.0... 
Error: Could not symlink include/node/common.gypi
/usr/local/include/node is not writable.

then I launched the command brew link --overwrite --dry-run node who returned the following message to me:

Would remove:
/usr/local/include/node/common.gypi
/usr/local/include/node/config.gypi
/usr/local/include/node/libplatform/libplatform-export.h
/usr/local/include/node/libplatform/libplatform.h
/usr/local/include/node/libplatform/v8-tracing.h
/usr/local/include/node/node.h
/usr/local/include/node/node_api.h
/usr/local/include/node/node_api_types.h
/usr/local/include/node/node_buffer.h
/usr/local/include/node/node_object_wrap.h
/usr/local/include/node/node_version.h
/usr/local/include/node/openssl/aes.h
/usr/local/include/node/openssl/asn1.h
/usr/local/include/node/openssl/asn1_mac.h
/usr/local/include/node/openssl/asn1t.h
/usr/local/include/node/openssl/bio.h
/usr/local/include/node/openssl/blowfish.h
/usr/local/include/node/openssl/bn.h
/usr/local/include/node/openssl/buffer.h
/usr/local/include/node/openssl/camellia.h
/usr/local/include/node/openssl/cast.h
/usr/local/include/node/openssl/cmac.h
/usr/local/include/node/openssl/cms.h
/usr/local/include/node/openssl/comp.h
/usr/local/include/node/openssl/conf.h
/usr/local/include/node/openssl/conf_api.h
/usr/local/include/node/openssl/crypto.h
/usr/local/include/node/openssl/des.h
/usr/local/include/node/openssl/dh.h
/usr/local/include/node/openssl/dsa.h
/usr/local/include/node/openssl/dtls1.h
/usr/local/include/node/openssl/e_os2.h
/usr/local/include/node/openssl/ebcdic.h
/usr/local/include/node/openssl/ec.h
/usr/local/include/node/openssl/ecdh.h
/usr/local/include/node/openssl/ecdsa.h
/usr/local/include/node/openssl/engine.h
/usr/local/include/node/openssl/err.h
/usr/local/include/node/openssl/evp.h
/usr/local/include/node/openssl/hmac.h
/usr/local/include/node/openssl/idea.h
/usr/local/include/node/openssl/lhash.h
/usr/local/include/node/openssl/md4.h
/usr/local/include/node/openssl/md5.h
/usr/local/include/node/openssl/mdc2.h
/usr/local/include/node/openssl/modes.h
/usr/local/include/node/openssl/obj_mac.h
/usr/local/include/node/openssl/objects.h
/usr/local/include/node/openssl/ocsp.h
/usr/local/include/node/openssl/opensslconf.h
/usr/local/include/node/openssl/opensslv.h
/usr/local/include/node/openssl/ossl_typ.h
/usr/local/include/node/openssl/pem.h
/usr/local/include/node/openssl/pem2.h
/usr/local/include/node/openssl/pkcs12.h
/usr/local/include/node/openssl/pkcs7.h
/usr/local/include/node/openssl/rand.h
/usr/local/include/node/openssl/rc2.h
/usr/local/include/node/openssl/rc4.h
/usr/local/include/node/openssl/ripemd.h
/usr/local/include/node/openssl/rsa.h
/usr/local/include/node/openssl/safestack.h
/usr/local/include/node/openssl/seed.h
/usr/local/include/node/openssl/sha.h
/usr/local/include/node/openssl/srp.h
/usr/local/include/node/openssl/srtp.h
/usr/local/include/node/openssl/ssl.h
/usr/local/include/node/openssl/ssl2.h
/usr/local/include/node/openssl/ssl3.h
/usr/local/include/node/openssl/stack.h
/usr/local/include/node/openssl/symhacks.h
/usr/local/include/node/openssl/tls1.h
/usr/local/include/node/openssl/ts.h
/usr/local/include/node/openssl/txt_db.h
/usr/local/include/node/openssl/ui.h
/usr/local/include/node/openssl/whrlpool.h
/usr/local/include/node/openssl/x509.h
/usr/local/include/node/openssl/x509_vfy.h
/usr/local/include/node/openssl/x509v3.h
/usr/local/include/node/uv.h
/usr/local/include/node/v8-platform.h
/usr/local/include/node/v8-profiler.h
/usr/local/include/node/v8-testing.h
/usr/local/include/node/v8-util.h
/usr/local/include/node/v8-value-serializer-version.h
/usr/local/include/node/v8-version-string.h
/usr/local/include/node/v8-version.h
/usr/local/include/node/v8.h
/usr/local/include/node/v8config.h
/usr/local/include/node/zconf.h
/usr/local/include/node/zlib.h
/usr/local/share/doc/node/gdbinit
/usr/local/share/doc/node/lldb_commands.py
/usr/local/share/man/man1/node.1
/usr/local/share/systemtap/tapset/node.stp
/usr/local/lib/dtrace/node.d
  1. now
    node -v gives me the message: -bash: /usr/local/bin/node: No such file or directory
    npm -v gives me the message env: node: No such file or directory

Now how do I proceed? I hope I followed the steps correctly.

You are on the npm forum and I am not a homebrew expert, so don’t treat my advice as gold. We are currently off topic. :slight_smile:

It looks like the brew link may have failed halfway through because of a permissions issue. The homebrew pattern is to take ownership of everything below /usr/local so that you do not need to use sudo with brew commands. I suggest this:

sudo chown -R $(whoami) /usr/local/include/node
brew link --overwrite node

(And if you get the same sort of error for files under /usr/local/share or /usr/local/lib then take ownership of those folders too.)

Then we can have a go at npm…

Thank you @shadowspawn,
if solving via Homebrew is offtopic I am open to other alternatives, currently the only thing I want is to solve the problem.

However, I launched the command:

sudo chown -R $(whoami) /usr/local/include/node

and apparently nothing happened.

Then I launched the command:

brew link --overwrite node

who returned the following message to me:

Linking /usr/local/Cellar/node/12.5.0... 
Error: Could not symlink share/doc/node/gdbinit
/usr/local/share/doc/node is not writable.

I had seen those folders in your earlier messages, I think we are winning.

sudo chown -R $(whoami) /usr/local/share
sudo chown -R $(whoami) /usr/local/lib
brew link --overwrite node

I must say that with the phrase “I think we are winning.” you snatched a smile from me :smiley:
I executed the following commands in the suggested order:

sudo chown -R $(whoami) /usr/local/share
sudo chown -R $(whoami) /usr/local/lib
brew link --overwrite node

and finally it seems that something has changed:

Linking /usr/local/Cellar/node/12.5.0... 202 symlinks created

Then I launched the command:

node -v

who returned the following message to me:

v12.5.0

Now to npm. Sticking with homebrew, not sure what state you are in so might be install or reinstall or link…

brew install npm

I launched the command:

brew install npm

who returned the following message to me:

Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/cask).
No changes to formulae.

Warning: node 12.5.0 is already installed and up-to-date
To reinstall 12.5.0, run `brew reinstall node`

I also don’t know if you can use it but after I launched the command:

npm -v

who returned the following message to me:

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

Error: Cannot find module 'semver'
Require stack:
- /usr/local/lib/node_modules/npm/lib/utils/unsupported.js
- /usr/local/lib/node_modules/npm/bin/npm-cli.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:625:15)
    at Function.Module._load (internal/modules/cjs/loader.js:527:27)
    at Module.require (internal/modules/cjs/loader.js:683:19)
    at require (internal/modules/cjs/helpers.js:16:16)
    at Object.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/unsupported.js:2:14)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Module.require (internal/modules/cjs/loader.js:683:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/usr/local/lib/node_modules/npm/lib/utils/unsupported.js',
    '/usr/local/lib/node_modules/npm/bin/npm-cli.js'
  ]
}

You got that message from npm install npm, not accidentally ran npm install node again?

But since npm was damaged, a reinstall is probably appropriate anyway:

npm reinstall  npm

The only commands I launched were the ones I wrote above:

brew install npm
npm -v

If I run the command:

npm reinstall npm

who returned the following message to me:

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

Error: Cannot find module 'semver'
Require stack:
- /usr/local/lib/node_modules/npm/lib/utils/unsupported.js
- /usr/local/lib/node_modules/npm/bin/npm-cli.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:625:15)
    at Function.Module._load (internal/modules/cjs/loader.js:527:27)
    at Module.require (internal/modules/cjs/loader.js:683:19)
    at require (internal/modules/cjs/helpers.js:16:16)
    at Object.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/unsupported.js:2:14)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Module.require (internal/modules/cjs/loader.js:683:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/usr/local/lib/node_modules/npm/lib/utils/unsupported.js',
    '/usr/local/lib/node_modules/npm/bin/npm-cli.js'
  ]
}

Oops, TYPO TYPO sorry. Please try this:

brew reinstall  npm

I used the command:

brew reinstall npm

and finally:

==> Reinstalling node 
==> Downloading https://homebrew.bintray.com/bottles/node-12.5.0.sierra.bottle.tar.gz
Already downloaded: /Users/fuzz/Library/Caches/Homebrew/downloads/f528bb4b63247b3b81752dd6132607868fa3074c6861d60bdf27703ca6868c16--node-12.5.0.sierra.bottle.tar.gz
==> Pouring node-12.5.0.sierra.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
==> Summary
🍺  /usr/local/Cellar/node/12.5.0: 4,505 files, 52.7MB

I then tried to launch:

node -v
npm -v

and I got respectively:

v12.5.0
6.9.0

Then I tried to initialize a folder and install a module like Stylelint.

npm init
npm install stylelint

and everything went well!
I’m really grateful @shadowspawn :star_struck:

1 Like

Now that we’ve solved the problem, I have a couple of questions:

  1. When in the future I update Homebrew and the Formulae lists with the command:
    brew update
    and I will launch the upgrade:
    brew upgrade
    will also nodejs and npm be updated correctly?

  2. In simple words, what happened to understand in the future what to do to avoid problems of this kind?

  3. One of the last steps was to launch the command:
    brew reinstall npm
    but npm is not installed together with nodejs? So it doesn’t update when we update nodejs?

If finally you believe that the title of this discussion should be changed to a more correct one, tell me as well. I would like this discussion to help other users with this problem.

Thanks again for your help.

  1. Yes (hopefully!)

  2. I suggest you install new versions of a program just one way. So if you are using brew to manage npm then only use brew. Don’t use “npm install -g npm” (which is using npm to manage npm, and will confuse brew).

On a related note, doing things the homebrew way, don’t use sudo when installing global modules with npm.

(I do not know why “sudo npm install -g npm” did not work, but that was the command that changed the ownership of some of the files in /usr/local to root and caused some of the brew problems.)

  1. Actually after a little digging I think you are right, npm is installed with node normally by homebrew. But I am guessing the broken state you started in meant it needed a reinstall of node/npm to fix it.

Fingers crossed :grin:

You’re right, as a novice I thought there was a certain interoperability between npm and brew, but it’s not like that. Clearly everyone handles his “modules” differently.
Moreover, the problem occurred just after launching the command:
npm install -g npm

Like for example npm install --global csslint right? If the answer is yes I have never used sudo for the npm modules so far (except when I launched sudo npm install -g npm).

I confirm that it is so. To learn something from this mishap I tried to install Homebrew, then Node.js (via Homebrew) on a clean virtual machine, and the result is that installing Node.js automatically installs npm, both working properly.

Thanks again for your patience.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.