8

I was trying to upgrade Node by following the instruction from https://tecadmin.net/upgrade-nodejs-via-npm/. I did sudo npm cache clean -f, sudo npm install -g n, and sudo n stable. After the above commands, I stupidly did sudo ln -sf /usr/local/n/versions/node/11.8.0/bin/node /usr/bin/node. After this command, I checked the /usr/local/n/versions/node folder. It had 10.15.3, so I typed sudo ln -sf /usr/local/n/versions/node/10.15.3/bin/node /usr/bin/node.

Right now, node -v gives me 10.15.3. However, npm -version gives me Segmentation fault (core dumped). Other commands like npm update -g also gives me the same error message.

Other posts about the Segmentation fault (core dumped) error don't seem to be directly related to my problem.

How can I fix this?

I am using Ubuntu 19.04.

Thank you.

Michele Dorigatti
  • 811
  • 1
  • 9
  • 17
Jeong Kim
  • 481
  • 3
  • 9
  • 21

5 Answers5

29

I faced The same Issue just now, This because of conflicting the npm of node and nodejs

so remove nodejs by below commands

sudo apt-get remove nodejs

this will remove all nodejs related Packages,

after above command you have to run autoremove command just like that

sudo apt autoremove

this will solve the problem,

Now you just have node pacakge in your machine, and NPM (segmentation dumped) error will also resolves.

you can more dig about What is segmentation fault

Adarsh Sharma
  • 526
  • 5
  • 11
  • I'm glad I could help. – Adarsh Sharma May 11 '19 at 21:07
  • 2
    it removed all nodejs, npm, node for me. So had to install again – Yusuf Jul 01 '20 at 09:34
  • Not working for me, can you help me a little bit here? – Monkey D. Luffy Sep 27 '20 at 12:03
  • This works for me when my `npm pack` command failed. However, it does removes all npm related packages installed by `apt-get` but since I manage npm using conda, this isn't an issue for me. – taper Dec 18 '20 at 12:05
  • This must be the funniest error I can encounter in a released version of a famous software... Every execution of npm, with or without any options, yields segfault immediately, and it is resolved after remove all nodejs related packages as described in this answer. – LIU Qingyuan Apr 22 '21 at 02:02
4

Try to run npm rebuild to recompile your dependencies.

0

This is caused by "binary incompability" between your compiled binaries and the libraries they are linked against. My list of things that can cause this

  • Upgrading nodejs, since you need to rebuild the code from clean state
  • Docker for example if you are sharing or copying node_modules from Mac to Linux or from Ubuntu to Alpine Linux
  • Docker multistage builds forget the libraries you had in the earlier stage unless you explicitly copy them
  • Vagrant for the same reason as Docker. By chance it might work if you have the same Linux distribution running on the same version inside and outside your virtual machine, over which node_modules is shared over a shared folder or a volume, which is more common on Vagrant

Short fix that works most often:

rm -rf node_modules
PHZ.fi-Pharazon
  • 1,479
  • 14
  • 15
0

Consider using Node Version Manager. Once installed you an easily upgrade downgrade node versions on the fly, with commands like NVM use 16 or NVM use 18.Sometimes I get this segmentation fault error and just quickly switch back to Node 16.

Greggory Wiley
  • 660
  • 6
  • 16
-3

Try to run the following two commands

sudo apt --auto-remove purge npm

sudo apt --auto-remove purge nodejs

After executing these commands npm was working without installation.

m4n0
  • 29,823
  • 27
  • 76
  • 89