308

I always get this error message when I run "Ionic start project name":

Error message

Running command - failed![ERROR] An error occurred while running npm install (exit code 1):

    module.js:471
        throw err;
        ^

    Error: Cannot find module '../lib/utils/unsupported.js'
        at Function.Module._resolveFilename (module.js:469:15)
        at Function.Module._load (module.js:417:25)
        at Module.require (module.js:497:17)
        at require (internal/module.js:20:19)
        at /usr/local/lib/node_modules/npm/bin/npm-cli.js:19:21
        at Object.<anonymous> (/usr/local/lib/node_modules/npm/bin/npm-cli.js:79:3)
        at Module._compile (module.js:570:32)
        at Object.Module._extensions..js (module.js:579:10)
        at Module.load (module.js:487:32)
        at tryModuleLoad (module.js:446:12)
dsapalo
  • 1,819
  • 2
  • 19
  • 37
inxoy
  • 3,484
  • 2
  • 13
  • 21

22 Answers22

843

Try to remove /usr/local/lib/node_modules/npm and reinstall node again. This should work.

On MacOS with Homebrew:

sudo rm -rf /usr/local/lib/node_modules/npm
brew reinstall node
dsapalo
  • 1,819
  • 2
  • 19
  • 37
Vincent Ducastel
  • 10,121
  • 2
  • 17
  • 11
  • 1
    if anyone wants to get there via finder: https://knowledge.autodesk.com/support/smoke/troubleshooting/caas/sfdcarticles/sfdcarticles/How-to-view-hidden-system-folders-in-Mac-OS-X-s.html – A.com Mar 07 '18 at 16:07
  • 14
    If you are using brew then you can use: `brew unistall --force node` and `brew install node` – Nico Mar 13 '18 at 12:49
  • 16
    @Nico the first command has a typo. The correction will be `brew uninstall --force node`. – Jayant Bhawal Apr 16 '18 at 14:58
  • 11
    why it's happening? – Gaurav Paliwal Jul 09 '18 at 13:35
  • 4
    After executing the above commands, now getting >> -bash: /usr/local/bin/npm: No such file or directory – Tarun Nov 12 '18 at 00:13
  • 1
    In my case just had to do a `yarn cache clean` (likely from upgrading from 1.9 to 1.14). – Leo Jan 04 '19 at 14:12
  • Mac OS Mojave, brew installed under prior Mac OSX. I had issues installing node relating to permissions with instruction to run "brew postinstall node" to resolve failures. Once installed version of npm was older and npx not installed / not in bin. I used "brew doctor" to fix issues with the system, then "brew upgrade". Then ran the above to reinstall node. – Flipmedia Jun 17 '19 at 08:26
  • @rmpt, same issue as you now T.T – Jerry Chong Jul 21 '19 at 15:36
  • Might need to run sudo chown -R $(whoami) $(brew --prefix)/* – PathToLife Jul 26 '19 at 05:02
  • Having this issue in Windows PC as well . Any solution for that – AHAMED AAQIB May 06 '22 at 04:30
85

I followed the previous answers and reinstalled node. But I got this error.

Warning: The post-install step did not complete successfully You can try again using brew postinstall node

So I ran this command

sudo chown -R $(whoami):admin /usr/local/lib/node_modules/

Then ran

brew postinstall node
tacticalmovephase
  • 1,119
  • 9
  • 16
28

I received a similar error and now have it working.

First make sure you have the latest version

brew update

Remove your previous instance of node:

brew uninstall node

Then reinstall the latest version:

brew install node

And then make sure it is symlinked into /usr/local if it isn't already. You would get an error to let you know to complete this step.

brew link --overwrite node 

More details on how to install/upgrade node are also available.

Denae
  • 381
  • 3
  • 7
19

On Mac OS X (10.12.6), I resolved this issue by doing the following:

brew uninstall --force node
brew install node

I then got an error complaining that node postinstall failed, and to rerun brew postinstall node

I then got an error:

permission denied @ rb_sysopen /usr/local/lib/node_modules/npm/bin/npx

I resolved that error by:

sudo chown -R $(whoami):admin /usr/local/lib/node_modules

And now I don't get this error any more.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
  • 1
    I had to repeat this a few times, on different folders that either needed to be created or needed to have their ownership changed (to my user). Each time I would run `brew postinstall node` to find the next problem, fix that, repeat. – T. Kim Nguyen Apr 07 '18 at 22:11
10

If you are using "n" library @ https://github.com/tj/n . Do the following

  echo $NODE_PATH

If node path is empty, then

sudo n latest    - sudo is optional depending on your system

After switching Node.js versions using n, npm may not work properly.

curl -0 -L https://npmjs.com/install.sh | sudo sh
echo NODE_PATH

You should see your Node Path now. Else, it might be something else

Philip E
  • 838
  • 9
  • 15
10

The error Cannot find module '../lib/utils/unsupported.js' is caused by require('../lib/utils/unsupported.js') in ./lib/node_modules/npm/bin/npm-cli.js.

According to the nodejs require docs, the required module is searched relative to the file, as it starts with ../.

Thus, if we take the relative path ../lib/utils/unsupported.js starting from ./lib/node_modules/npm/bin/npm-cli.js, the required module must reside in ./lib/node_modules/npm/lib/utils/unsupported.js. If it is not there, I see two options:

  • the installation is corrupt, in which case Vincent Ducastel's answer to reinstall node might work
  • npm is no symlink to ./lib/node_modules/npm/bin/npm-cli.js. This is what caused the error in my setup. If you call npm, it will typically find it be searching it in the directories listed in the PATH env var. It might for example be located in ./bin. However, npm in a ./bin directory should only be a symlink to the aforementioned ./lib/node_modules/npm/bin/npm-cli.js. If it is not a symlink but directly contains the code, somewhere in the installation process the symlink got replaced by the file it links to. In this case, it should be sufficient to recreate the symlink: cd ./bin; rm npm; ln -s ../lib/node_modules/npm/bin/npm-cli.js npm (update: command fixed, thx @massimo)

All answers that suggest to check the NODE_PATH or the npmrc config should be ignored, as these are not considered when searching modules relatively.

franzlst
  • 155
  • 1
  • 9
  • I had similar issue with the wrong/missing symlink, but actually the syntax to recreate is: `ln -s ../lib/node_modules/npm/bin/npm-cli.js npm` – Massimo Sep 07 '20 at 16:30
8

As mentioned earlier.

 sudo rm -rf /usr/local/lib/node_modules/npm
 brew uninstall --force node                
 brew install node
jackotonye
  • 3,537
  • 23
  • 31
7

Tried all above/older brew installation answers, none is working for my laptop.

Only below method could fix my issue.

1) Run following commands:

sudo rm -rf /usr/local/lib/node_modules/npm
brew uninstall --force node      

2) Then, proceed to Node.js Official Website https://nodejs.org/en/download/current/ to download latest package for new installation.

3) Run your npm command again, which should longer have any errors.

This method is working on macOS Mojave Version 10.14.4.

Jerry Chong
  • 7,954
  • 4
  • 45
  • 40
5

https://nodejs.org/en/

Simply download node from the official website, this worked for me! :)

C Williams
  • 850
  • 12
  • 19
4

On Windows:

Remove the npm folder in ~/AppData/Roaming

CoutWout
  • 53
  • 5
2

Yes you should re-install node:

sudo rm -rf /usr/local/lib/node_modules/npm
 brew uninstall --force node                
 brew install node
Walterwhites
  • 1,287
  • 13
  • 9
2

Simply follow three steps;

  1. Clear npm cache forcefully:

    npm cache clean -f

  2. Install n package globally using npm:

    npm install -g n

  3. Install from any of three options:

    a. sudo n stable (get the stable version)

    b. sudo n latest (get the latest version of node)

    c. sudo n x.x.x (get the specific version of node)

M--
  • 25,431
  • 8
  • 61
  • 93
Laxmikanta Nayak
  • 375
  • 2
  • 10
1

I got this error by mixing install/update methods: installed node via downloading package from website and later I used brew to update.

I fixed by uninstalling the brew version :

brew uninstall --ignore-dependencies node

Then I went back to node website and downloaded and installed via the package manager: https://nodejs.org/en/download/ For some reason, no amount of trying to reinstall via brew worked.

jleatham
  • 456
  • 8
  • 17
1

I was running into a similar issue where the whole ../lib/utils directory couldn't be found when I tried executing Mocha via npm test. I tried the mentioned solutions here with no luck. Ultimately I ended up uninstalling and reinstalling the Mocha package that was a dependency in the npm project I was working in and it worked after that. So if anyone's having this issue with an npm package installed as a dependency, try uninstalling and reinstalling the package if you haven't already!

Snap
  • 492
  • 5
  • 14
1

I solved this issue by running below command

nvm install node --reinstall-packages-from=node

prito
  • 29
  • 6
0

In my case it was $NODE_PATH missing:

NODE="/home/ubuntu/local/node" #here your user account after home
NODE_PATH="/usr/local/lib/node_modules" 
PATH="$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:$NODE/bin:$NODE/lib/node_modules"

To check just echo $NODE_PATH empty means it is not set. Add them to .bashrc is recommended.

SkorpEN
  • 2,491
  • 1
  • 22
  • 28
0

On fedora 27 I solved the problem by doing this:

sudo rm -f  /usr/local/lib/node_modules/npm
sudo dnf reinstall nodejs
tbo47
  • 2,688
  • 3
  • 20
  • 11
0

This is a helpful video and blog post about removing node from your computer OS. It is a different method of removal based on how you installed node in the first place (brew vs. binary file downloaded from https://nodejs.org/en/

  • if you installed node with Homebrew then brew uninstall node will work. Verify that with running a node -v command in your terminal.

  • Otherwise and if you have installed the binary file from nodeJS's websitethen you have to run this command in your terminal: sudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man/*/node.*}. Again, verify that with running a node -v command.

  • In both cases, successful removal of node should result in bash not recognizing what node is if it is completely removed

Omar
  • 49
  • 1
  • 6
  • 2
    A link to a solution is welcome to *support* an answer, but the answer you post should be completely self-contained, i.e. not require visitors to click through to another site which may contain unexpected or malicious content, or no longer exist at all. – tripleee May 02 '18 at 10:09
0

In my macOS (10.13.3), I got it solved after reinstalling Node version manager.

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
source ~/.bashrc
user9869932
  • 6,571
  • 3
  • 55
  • 49
0

This may happen when the npm/lib folder got emptied for some reason (could also happen due to permission issues in the last usage).

A reinstallation of the node could solve the issue (as stated on other answers here), but I would suggest using a wonderful tool called nvm (Node Version Manager), which is able to manage multiple version of node and npm - this is mostly useful on dev machines with more than one projects require different versions of node.

When you install nvm, this message will go away and you will have the latest version of node and npm to use.

In order to see the list of currently installed node versions in your nvm, just run:

nvm list

In order to install and use a new node version, run:

nvm install <node_version>

For example to install latest version of node 10.x, run:

nvm install 10

In order to switch to currently installed version, run:

nvm use <node_version>

In order to switch to system's original node version, just run:

nvm use system

Hope this helps.

Good luck!

Slavik Meltser
  • 9,712
  • 3
  • 47
  • 48
0

I solve this issue by removing node_modules and then reinstall node stable version.

-1

You can run this command it will automatically remove the previous version of npm and install new version for details https://github.com/npm/cli

curl -qL https://www.npmjs.com/install.sh | sh