简体   繁体   中英

Npm cannot be installed successfully while installing nodejs

I'm new in using nodejs. I installed Node.js using this command

sudo apt-get install -y nodejs

Normally, when Node.js is installed, npm will be automatically installed. But when checking the npm version i get this result

在此处输入图片说明

How can i fix it?

  1. Remove the legacy NodeJS by calling sudo apt-get remove --purge nodejs -y because it seems to be outdated
  2. Follow the instruction on the NodeJS page here

NodeJS from the Linux repositories are mostly outdated. The newest version is v8.x

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.

#!/bin/sh
# 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

i've got the answer from the below source: link

I had the same issue with npm on Linux Mint 20:

$ npm -v
Segmentation fault (core dumped)

And i managed to resolve it by removing the current nodejs/npm first:

sudo apt-get remove --purge nodejs npm -y

And then following the official Installation instructions for Ubuntu and Node.js Current (v14.x) or any other version you like:

curl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs

You may also run autoremove if any related packages are left uninstalled:

sudo apt autoremove

I have the same problem in Mint 20 that NodeJS installed from Mint Software Manager without npm and I solved it by installing the npm using the command line

sudo apt install npm

and now it works fine with me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM