Skip to content

Instantly share code, notes, and snippets.

@16hands
Last active July 31, 2017 01:59
Show Gist options
  • Save 16hands/b6ef10b8c97aa10c97001e87979f7a3d to your computer and use it in GitHub Desktop.
Save 16hands/b6ef10b8c97aa10c97001e87979f7a3d to your computer and use it in GitHub Desktop.
NPM on Mac with Brew throws error module.js:457, how to fix

Fixing npm on MacOS when installed with Homebrew

Google shows this gist at the top of most searches GitHub https://gist.github.com/DanHerbert/9520689 which is unfortunately very old and doesn't work now.

After doing an upgrade with homebrew, we got this error:

module.js:457
    throw err;
    ^

Error: Cannot find module 'semver'
    at Function.Module._resolveFilename (module.js:455:15)
    at Function.Module._load (module.js:403:25)
    at Module.require (module.js:483:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/unsupported.js:2:14)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)

Most of the node/npm forums say this means npm is really dead, and to start again.

The stastackoverflow below gave us a clue to fixing this, the issue is that if you fully remove node with brew, even with the --force, it still leaves the /usr/local/lib/node_modules directory in place so upgrades get hosed.

This seems to more evident in Mac OS Sierra.

https://stackoverflow.com/questions/39503926/npm-module-js457-throw-err-error-cannot-find-module-npmlog-on-a-mac-using

brew uninstall --force node
rm -rf /usr/local/lib/node_modules
rm /usr/local/bin/npm
brew install node
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment