nodejs, npm works fine before. When I try to upgrade it

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

And type npm -v, it throw "Segmentation fault". I restart system and I type a same command again, again it show same error. So I remove nodejs and npm package from ubuntu 14.04 through ubuntu Software Center and reinstall it again.

Then I try to install the phonegap through npm package using a command

sudo npm install -g phonegap

But it doesn't return any progress. So I check the npm version, it throws an error "Segmentation fault". I also try sudo npm -v. It return nothing.

So I decide to remove the npm from ubuntu 14.04 using the command

sudo apt-get remove npm 

Then I type sudo npm install -g phonegap it can't throw npm package is not available.


My Question is:

  1. The package is not there, then why i type npm -v, it return "Segmenatation Fault"
  2. If package not remove, then when i type sudo npm install -g phonegap, why It cannot install phonegap.

Help me to solve this issue. Or provide a way to unistall nodejs, npm, phonegap, expressjs, less clean way and reinstall it.




As per @chj1axr0 answer, the script throw an error after nodejs install

enter image description here

  • Are you trying to install the latest version of NodeJS? – chj1axr0 Jul 11 '15 at 9:17
  • @chj1axr0 yes...when i tried to upgrade nodejs, after that I get an issue of Segmentation fault – Tamil Selvan C Jul 11 '15 at 9:19
  • @chj1axr0 I tried to uninstall it many times and reinstall it, no way its works. Can you provide a way to clean uninstall and install nodejs, phonegap, expressjs again. – Tamil Selvan C Jul 11 '15 at 9:21
  • Other then phonegap and express js do you have any other node modules installed? You can check by going here /usr/local/lib/node_modules if they are installed globally this will help in determining the next steps – chj1axr0 Jul 11 '15 at 9:47
  • By the way what version of Ubuntu are you trying to set this up on – chj1axr0 Jul 11 '15 at 10:09

I have googled around and it seems (for some) that there is a bug in the latest Ubuntu software version of Nodejs that causes the segmentation fault that you are haveing

The best way to install it is by getting node from the source and compiling it.

I have setup a simple script on a Github gist that will take care of it.

# Update System
echo "System Update"
apt-get -y update
echo "Update completed"
# Installing the applications needed to build Nodejs
apt-get -y install libssl-dev git-core pkg-config build-essential curl gcc g++ checkinstall
# Download & Unpack Node.js - v7.3.0
echo "Download Node.js - v7.3.0"
mkdir /tmp/node-install
cd /tmp/node-install
wget https://nodejs.org/dist/latest/node-v7.3.0.tar.gz
tar -zxf node-v7.3.0.tar.gz
echo "Node.js download & unpack completed"
# Install Node.js
echo "Compiling and installing Node.js"
cd node-v7.3.0
./configure && make && checkinstall --install=yes --pkgname=nodejs --pkgversion "7.3.0" --default
echo "Node.js install completed! Deleting The /tmp files"
rm -rf /tmp/node-install
echo "If you have made it this far in the script with no errors popping up all is well have a good day" 

Make sure and use sudo chmod a+x /path/to/file/install_nodejs_latest.sh to make sure it will start. Then use sudo sh /path/to/file/install_nodejs_latest.sh to start it. (It must me ran with sudo for all the commands to fire correctly)

The script removes the old nodejs, npm and all node modules.

It will update npm at the end of the script.

Every once and awhile it is recommended to do sudo npm install -g npm (If you want the latest) because npm upgrades faster then node js does

after the script is complete run npm -v and node -v If the versions pop up then run sudo npm install -g phonegap,sudo npm install -g cordova,sudo npm install -g less also if you want to update any of the modules in the future all you have to do is install it again and that will override the preveous one. expressjs is for an app by app bases so it's not recomended to install it globally

  • OK. I try it and let you know when it finish – Tamil Selvan C Jul 11 '15 at 13:03
  • See my Edit1. I get an error after install nodejs in script – Tamil Selvan C Jul 11 '15 at 13:34
  • When you do npm -v or node -v what does the output say? It may just be having an error on installing the modules – chj1axr0 Jul 11 '15 at 13:39
  • npm - v returns 2.11.3 and node -v returns v0.12.7 – Tamil Selvan C Jul 11 '15 at 13:41
  • Ok so Node is installed and so is npm try doing sudo install -g phonegap or sudo npm install -g cordova and see the output of that – chj1axr0 Jul 11 '15 at 13:43
  1. Try removing with apt-get remove --purge. Maybe some files got left around.

  2. check the output of which npm.

  3. How did you install nodejs? I think the upstream version packages npm along with with node, whereas they're separate in ubuntu. If you installed upstream npm, removing npm with apt just switched you to using upstream npm. If the problem is with corrupted packages which node/npm loads on startup, switching won't fix it. Maybe removing all node/npm stuff from your system and starting fresh will. Who knows. It's node.

  • I install nodejs from ubuntu software center – Tamil Selvan C Jul 11 '15 at 7:41
  • 1
    see my edit in my question. When I type phonegap -v it return Segemation error. Can you explain how to remove all nodejs and npm, phonegap, expressjs, less – Tamil Selvan C Jul 11 '15 at 7:46

I have the same problem and solved it.(my OS is ubuntu 19.4)

the reason that this problem happens, is you installed nodejs and npm separately.

so At first, you should remove nodejs and npm.

sudo apt --auto-remove purge npm
sudo apt --auto-remove purge nodejs

and then install just nodejs

sudo apt-get install nodejs

At last, do following commands for checking nodejs and npm

node -v
npm -v

Note: Because nodejs and npm are packaged together. you do not need to install npm seperately.

  • This worked for me on Ubuntu 19 – Peter Drinnan Jul 10 '19 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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