32

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 Answers 10

17

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
2
  • Thanks for the link. It saved me a lot of trouble.
    – Willa
    Mar 25, 2016 at 17:11
  • 4
    On OS X El Capitan while installing npm 3.9.3, I had a npm: command not found after doing this. I had do add export PATH="$HOME/.npm-packages/bin:$PATH" to my .bash_profile
    – clemlatz
    May 30, 2016 at 8:22
17

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

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

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.

4
  • 2
    Thanks, I had the exact same issue. I downloaded a pre-built package from nodejs.org, extracted it on my mac, then uploaded it to my target system via SFTP. Once on the target system, the symlink was broken, resulting in the above error message. Removing the broken link: node/bin/npm and replacing it with ln -s node/lib/node_modules/npm/bin/npm-cli.js node/bin/npm has fixed the issue.
    – bagonyi
    Feb 7, 2017 at 18:12
  • I should have mentioned that using ln -nfs should remove the original file and replace it with the soft link. Saves you a step! Although it can sometimes fail. Feb 7, 2017 at 18:38
  • I used scp to copy from one server to another and experienced this same issue.
    – Jeremy
    Mar 10, 2017 at 20:16
  • This answer gave me the solution when downloading the macOS binaries straight from the website, although it took a while to get to the root of it: use cp -R instead of cp -r
    – Del
    Jul 12, 2017 at 14:04
3

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

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

3

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
2
  • Thanks, this saved me some time since I have your same setup. The error comes back every time I try to update via npm though Apr 2, 2018 at 7:57
  • 2
    Yeah - I think you just can't - if you're using dnf then you have to wait until there's a new version of npm via dnf
    – icc97
    Apr 2, 2018 at 15:14
0

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.