npm: module.js:457 throw err; Error: Cannot find module 'npmlog' on a mac using brew

11,137

Solution 1

Alexander O'Mara's comment helped me solve this. The solution:

Run brew uninstall --ignore-dependencies node to forcefully uninstall node even if there are other modules that require node.

Once this is completed, run brew uninstall --force node to remove all the versions of node on your machine, if there is more than one version running.

At last run the following commands to remove all files and install node again:

rm -rf /usr/local/lib/node_modules
rm /usr/local/bin/npm
brew install node

Solution 2

In your Linux box;

You may need to add the following link

  1. Go to NPM installtion dir << cd ~/node-v6.9.2-linux-x64/>> (I'm using NODE Current LTS 6.9.2)

  2. look for the file npm-cli.js in lib/node_modules/npm/bin/npm-cli.js (File name can be cli.js or npm-cli.js)

  3. cd ~/node-v6.9.2-linux-x64/bin

  4. mv npm npm_old

  5. Crate a soft link in ~/node-v6.9.2-linux-x64/bin ln -s PATH-TO/npm-cli.js npm

Try npm -v

Optional if you need to update npm version pls do npm install npm@latest -g

Share:
11,137
Dave
Author by

Dave

Updated on June 07, 2022

Comments

  • Dave
    Dave almost 2 years

    I made the mistake (?) of following the instructions on the npm getting started page and ran

    sudo npm install npm -g
    

    Now, no matter what I do, any execution of npm results in this error, whether I run it as sudo or not. Uninstalling and reinstalling node has not helped.

    $ sudo npm
    
    module.js:457
        throw err;
        ^
    
    Error: Cannot find module 'npmlog'
        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 /usr/local/lib/node_modules/npm/bin/npm-cli.js:19:13
        at Object.<anonymous> (/usr/local/lib/node_modules/npm/bin/npm-cli.js:75:3)
        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)
    

    I am on a mac and installed node using homebrew.