node.js on Mac OS X npm “Error: No such module”

2019-05-03 14:41发布

问题:

I have installed node.js v0.8.8 both from the pkg installer and compiled from source. Yet I get the following error when trying to run npm. Any help would be appreciated.

>  $ npm --help
> 
> /usr/lib/node_modules/npm/lib/utils/config-defs.js:5   , stdio =
> process.binding("stdio")
>                     ^ Error: No such module
>     at Object.<anonymous> (/usr/lib/node_modules/npm/lib/utils/config-defs.js:5:21)
>     at Module._compile (module.js:449:26)
>     at Object.Module._extensions..js (module.js:467:10)
>     at Module.load (module.js:356:32)
>     at Function.Module._load (module.js:312:12)
>     at Module.require (module.js:362:17)
>     at require (module.js:378:17)
>     at Object.<anonymous> (/usr/lib/node_modules/npm/lib/utils/ini.js:43:18)
>     at Module._compile (module.js:449:26)
>     at Object.Module._extensions..js (module.js:467:10)

回答1:

You are most likely using an old NPM version somehow. see: npm issue



回答2:

I had the exact same issue upgrading from Node.JS 0.4 to 0.8. What worked for me was the following:

The OSX Node.JS package installer erroneously does not remove the old 0.4 npm installation in /usr/bin, which then conflicts with the new 0.8 one in /usr/local/bin. It just overwrites whatever is there already. I nuked the old one by deleting it, but to make sure, I decided a clean install would be better:

curl https://npmjs.org/install.sh | sudo clean=yes sh

There was one last problem: after the install, even though a which npm correctly returns /usr/local/bin/npm, bash kept trying to execute from /usr/bin. This is because on OSX, bash has hashed the executable name. The hashing saves repeated searches of all directories in your PATH every time you execute common programs. A hash -r at the command line took care of this issue.