32
votes

Today is my first day of my first job and after setting up my development environment by updating node and npm on my Mac (OSX) I seem to have broken something. Every time I try to use npm in my commandline (npm init, npm install, etc.) I get this error message:

module.js:339

throw err;

Error: Cannot find module 'npmlog'

at Function.Module._resolveFilename (module.js:337:15)

at Function.Module._load (module.js:287:25)

at Module.require (module.js:366:17)

at require (module.js:385:17)

at /usr/local/lib/node_modules/npm/bin/npm-cli.js:20:13

at Object. (/usr/local/lib/node_modules/npm/bin/npm->cli.js:76:3)

at Module._compile (module.js:425:26)

at Object.Module._extensions..js (module.js:432:10)

at Module.load (module.js:356:32)

at Function.Module._load (module.js:311:12)

There isn't much of an error message to go off and the stack trace isn't helping me all that much either. Here's what I have tried:

  1. Searched around Stackoverflow and though there are similar problems, people have been able to use npm <something> to solve their problem, which I cannot do.
  2. Uninstalling and reinstalling npm - Didn't work because I can't use npm command at all so things like sudo npm uninstall npm -g don't work.
  3. Removed npmlog directory from .npm directory and then cloned the github repo directly to the directory again.

Hopefully one of you have run into this before or have an idea of how to approach it. Any help is appreciated.

10

10 Answers

17
votes

Here is one good link Fixing npm On Mac OS X for Homebrew Users.

with following commands when try to upgrade node.js under mac

rm -rf /usr/local/lib/node_modules
brew uninstall node
brew install node --without-npm
echo prefix=~/.npm-packages >> ~/.npmrc
curl -L https://www.npmjs.com/install.sh | sudo sh
17
votes

Here are the steps that worked for me:

$ sudo rm -rf /usr/local/bin/npm
$ sudo rm -rf /usr/local/lib/node_modules/npm
$ brew uninstall node
$ brew install node
11
votes

Try curl -0 -L http://npmjs.org/install.sh | sudo sh to follow redirects. Also notice I added sudo before the sudo sh command depending on your users permissions.

5
votes

I've just encountered this issue on a Mac, and on closer inspection it seems to me that the problem is that inside the Node application directory in certain circumstances the link in the node/bin directory to npm in lib/node_modules/npm/bin is replaced by the file itself and that is why it fails (and why all the various script methods fix it, because they correctly retain the file link).

If I copy the contents from node-v6.9.4-darwin-x64 with cp then I get this problem, but if I copy the files using the Finder, then I don't. So, you can fix it by manually recreating the link with ln if you don't want to use the Finder (alternatively, mv or rsync -a could work too; I haven't tested them).

Hope this helps someone coming here for this problem.

Edit: I've just accidentally found the reason for the problem on Macs (for my particular scenario, at any rate): I've always used cp -r to copy directories. This is because I am old, and don't learn new things any more. The correct way is cp -R, which correctly copies everything, as it should.

3
votes

I didn't necessarily "fix" the problem as much as just start over. I used this post to do so: How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)

If anyone else knows how to actually fix the problem or knows what I did to cause it I would still be very interested.

3
votes

in my condition, I just brew uninstall node and download from the official site and worked for me

3
votes

I hit this on Fedora 27 after I ran sudo npm install npm -g.

The problem was that I had initially installed Node.js via sudo dnf install nodejs which installs npm into /usr/local/bin and /usr/local/lib. However running sudo npm install installs a separate version in /usr/bin/

Running which npm shows up that the extra npm is installed. Then the following cleaned everything for me:

$ sudo rm /usr/bin/npm
$ sudo rm -rf /usr/lib/node_modules/npm
$ dnf remove -y nodejs
$ dnf install -y nodejs
0
votes

I encountered this same error. This was on a server not connected to the internet. I had seen a thread on Github that mentioned sometimes symbolic links can be removed while performing copies or other file operations. So I did a "find" on the node directory for symbolic links of the problematic server and none where found. Doing the same on a working server found symbolic link files.

I went to NODE_HOME/bin and did an ls -l

Sure enough I had a file for npm. Performed the same steps on a working server and the npm file was a symbolic link. To fix the problematic server, I performed the following:

rm npm

ln -s ../lib/node_modules/npm/bin/npm-cli.js npm

0
votes

Got the same error while installing protractor in REDHAT linux updated the

npm

file at

/node-v6.10.2-linux-x64/bin/npm

for the module path as below:

Code Before

var log = require('npmlog')

Code After

var log = require('/node-v6.10.2-linux-x64/lib/node_modules/npm/node_modules/npmlog')
0
votes

For me, I ran brew uninstall node and then download the latest version from nodejs.org and it worked.