[Node.js] Cannot install packages using node package manager in Ubuntu


Try linking node to nodejs. First find out where nodejs is

whereis nodejs

Then soft link node to nodejs

ln -s [the path of nodejs] /usr/bin/node 

I am assuming /usr/bin is in your execution path. Then you can test by typing node or npm into your command line, and everything should work now.


NodeJS interpreter name(node) on Ubuntu has been renamed to nodejs because of name conflict with another package. Here's what the readme.debian says:

The upstream name for the Node.js interpreter command is "node". In Debian the interpreter command has been changed to "nodejs".

This was done to prevent a namespace collision: other commands use the same name in their upstreams, such as ax25-node from the "node" package.

Scripts calling Node.js as a shell command must be changed to instead use the "nodejs" command.

However, using nodejs mucks up installing packages using npm. Package installation fails with the following error:

sh: 1: node: not found
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read /usr/share/doc/nodejs/README.Debian

How do I make npm understand that nodejs is already installed on the system but the interpreter name is different?

Here's another approach I use since I like n for easy switching between node versions.

On a new Ubuntu system, first install the 'system' node:

curl -sL https://deb.nodesource.com/setup | sudo bash -

Then install n module globally:

npm install -g n

Since the system node was installed first (above), the alternatives system can be used to cleanly point to the node provided by n. First make sure the alternatives system has nothing for node:

update-alternatives --remove-all node

Then add the node provided by n:

update-alternatives --install /usr/bin/node node /usr/local/bin/node 1

Next add node provided by the system (the one that was installed with curl):

update-alternatives --install /usr/bin/node node /usr/bin/nodejs 2

Now select the node provided by n using the interactive menu (select /usr/local/bin/node from the menu presented by the following command):

update-alternatives --config node

Finally, since /usr/local/bin usually has a higher precedence in PATH than /usr/bin, the following alias must be created (enter in your .bashrc or .zshrc) if the alternatives system node is to be effective; otherwise the node installed with n in /usr/local/bin takes always precedence:

alias node='/usr/bin/node'

Now you can easily switch between node versions with n <desired node version number>.

On Linux Mint 17, I tried both solutions (creating a symlink or using the nodejs-legacy package) without success.

The only thing that finally worked for me was using the ppa from Chris Lea:

sudo apt-get purge node-*
sudo apt-get autoremove 
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

This installed node version 10.37 and npm 1.4.28. After that, I could install packages globally.

Problem is not in installer
replace nodejs with node or change the path from /usr/bin/nodejs to /usr/bin/node

Uninstall whatever node version you have

sudo apt-get --purge remove node
sudo apt-get --purge remove nodejs-legacy
sudo apt-get --purge remove nodejs

install nvm (Node Version Manager) https://github.com/creationix/nvm

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash

Now you can install whatever version of node you want and switch between the versions.

For me the fix was removing the node* packages and also the npm packages.

Then a fresh install as:

sudo apt-get install autoclean
sudo apt-get install nodejs-legacy
npm install

  1. Install nvm first using:

    curl https://raw.githubusercontent.com/creationix/nvm/v0.11.1/install.sh | bash
  2. Run command

    source ~/.profile
  3. Now run this and this will show will all installed or other versions of packages:

    nvm ls-remote
  4. Installed packages will be in green. Install whatever version you want:

    nvm install 6.0.0
  5. Check where is not installed:

    which node
  6. Check current version:

    node -v
    n=$(which node);
    chmod -R 755 $n/bin/*; 
    sudo cp -r $n/{bin,lib,share} /usr/local

Simple solution from here

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

You can specify version by changing setup_x.x value, for example to setup_5.x