525

NodeJS interpreter name(node) on Ubuntu has been renamed to nodejs because of a 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 upstream, 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?

Ijas Ameenudeen
  • 9,069
  • 3
  • 41
  • 54
Sayem
  • 6,079
  • 4
  • 22
  • 26
  • 29
    You can try to `ln -s nodejs node` in `/usr/bin`. It's kind of hack though. – vbo Jan 16 '14 at 17:07
  • 1
    @vbo you can add that as an answer - it solve the problem! – Himel Nag Rana Aug 06 '14 at 11:55
  • 2
    @HimelNagRana I have another (better, accepted) answer. My initial comment was also transformed to answer (http://stackoverflow.com/a/21168305/539686). – vbo Aug 06 '14 at 12:17
  • 4
    Use NVM to install and manage Node.js versions, https://github.com/creationix/nvm, it's easy and convenient ! – Unitech Sep 23 '14 at 10:27
  • 2
    I highly recommend this solution: http://stackoverflow.com/a/38325376/3197383 to get control of the node and npm versions at any time and for any usage. – Rémi Becheras Nov 21 '16 at 16:25

19 Answers19

1111

TL;DR:

sudo apt-get install nodejs-legacy

First of all let me clarify the situation a bit. In summer 2012 Debian maintainers decided to rename Node.js executable to prevent some kind of namespace collision with another package. It was very hard decision for Debian Technical Committee, because it breaks backward compatibility.

The following is a quote from Committee resolution draft, published in Debian mailing list:

  1. The nodejs package shall be changed to provide /usr/bin/nodejs, not /usr/bin/node. The package should declare a Breaks: relationship with any packages in Debian that reference /usr/bin/node.

  2. The nodejs source package shall also provide a nodejs-legacy binary package at Priority: extra that contains /usr/bin/node as a symlink to /usr/bin/nodejs. No package in the archive may depend on or recommend the nodejs-legacy package, which is provided solely for upstream
    compatibility. This package declares shall also declare a Conflicts: relationship with the node package.

<...>

Paragraph 2 is the actual solution for OP's issue. OP should try to install this package instead of doing symlink by hand. Here is a link to this package in Debian package index website.

It can be installed using sudo apt-get install nodejs-legacy.

I have not found any information about adopting the whole thing by NPM developers, but I think npm package will be fixed on some point and nodejs-legacy become really legacy.

KlwntSingh
  • 1,084
  • 8
  • 26
vbo
  • 13,583
  • 1
  • 25
  • 33
  • 107
    To summarise: `sudo apt-get install nodejs-legacy` – Alf Eaton Jan 28 '14 at 00:42
  • 60
    I like answers which explain the situation thoroughly rather than just giving a command to execute blindly. Then I can copy that info into the comments in my environment setup script so I know what's going on a year from now when it doesn't work. – Mnebuerquo Oct 08 '14 at 18:28
  • 29
    As a person I find it incredulously inconvenient that node is not node, but instead nodejs, only on Debian. It's really, really, really amazingly stupid to splinter development tools like this. Basically, this decision created an exceptional situation for Debian in EVERY NODEJS PACKAGE! I've never heard of what "node" does otherwise, so I lack any sympathy ;( – Lodewijk Nov 27 '14 at 18:45
  • Doesn't work for me. I have still: sh: 1: node-pre-gyp: not found npm WARN This failure might be due to the use of legacy binary "node" – kabra Nov 04 '15 at 09:07
  • 1
    worked for me, but only after I ran `source ~/.profile` – Ron Klein Jan 12 '16 at 14:19
  • 4
    Unless I'm missing something, this was not a name*space* collision. There's no *namespace*, it was a simple *name* collision. Name*spaces* allow you to have the same name twice (or more) as long as each exists in a different name*space*. No such "spaces" exist for executable names in Linux or Unix. The closest thing is that you can have multiple directories in your $PATH variable, and the earliest one with a matching named executable is used. (But that's really very different from a namespace.) – iconoclast Jan 24 '16 at 19:35
  • This isn't working for me, even after running `source ~/.profile`. – huertanix Feb 02 '16 at 19:49
  • 7
    This is very annoying, even more so because the name conflict was with a ["Amateur Packet Radio"](https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions). – Suzana Feb 09 '16 at 21:44
  • When I try to `apt-get install nodejs-legacy` I get: `The following packages have unmet dependencies: nodejs-legacy : Depends: nodejs (>= 0.6.19~dfsg1-3~) but it is not going to be installed` – CodingWithSpike Dec 02 '16 at 02:09
  • @CodingWithSpike did you find the solution? – Fabrizio Bertoglio Apr 17 '17 at 09:27
  • @FabrizioBertoglio It's been a while, but the Node docs say that you want to install `nodejs` not `nodejs-legacy` : https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions then IIRC you have to manually symlink `nodejs` to `node` as mentioned in a comment on the original post. I think that's how I got it eventually working. – CodingWithSpike Apr 25 '17 at 13:23
  • 2
    @iconoclast It is a namespace collision in that it is a collision within the namespace of "executables in the `PATH`". – Mark Jul 29 '17 at 13:50
  • As someone building their first node project, I wish I had more than one upvote. – Lawrence Aiello Feb 22 '18 at 04:39
114

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.

leorex
  • 2,058
  • 1
  • 14
  • 15
  • 3
    -s creates symbolic link (which is often preferred choice), not hard link. – hyde Jan 16 '14 at 19:42
  • 20
    This works, but [vbo's answer](http://stackoverflow.com/a/21171188/4794) of installing the `nodejs-legacy` package is easier. – Don Kirkby May 05 '14 at 20:47
  • 2
    I second Don Kirkby's comment and vbo's answer. I think that vbo's answer should be the selected answer. – modulitos Jun 20 '14 at 16:41
  • 3
    I find it much more convenient to just create a symbolik link rather than installing the same software twice .... – weisk Feb 11 '15 at 11:03
  • 1
    @frankies read the answer, the `nodejs-legacy` doesn't reinstall the software, it just creates the symlink for you… – Daniel Aug 14 '15 at 09:07
  • 7
    **No.** Don't muck with `/usr/bin` unless you are `dpkg`. The proper solution (if you like this manual symlinking approach) is to **create the symlink in `/usr/local/bin`** which is designated for local installs and overrides. Anything you do manually in `/usr/bin` can and will be overwritten by the package manager, and/or will confuse it. – tripleee Dec 17 '15 at 09:52
  • Also, `whereis` doesn't seem to offer any advantage in this case over `type`, which has the distinct advantage of being a shell built-in, thus able to tell you about shell built-ins, aliases, and functions as well as binaries. – tripleee Dec 17 '15 at 09:54
  • The install of nodejs-legacy did not help and I could not create a symlink to `/usr/bin/node` because that exists already. The error message stays the same. – Suzana Feb 09 '16 at 21:40
  • @Daniel Well, I read the other answer and it wasn't very clear that nodejs-legacy created a symlink. Good to know though. – nick Apr 26 '16 at 05:08
46

You can also install Nodejs using NVM or Nodejs Version Manager There are a lot of benefits to using a version manager. One of them being you don't have to worry about this issue.


Instructions:


sudo apt-get update
sudo apt-get install build-essential libssl-dev

Once the prerequisite packages are installed, you can pull down the nvm installation script from the project's GitHub page. The version number may be different, but in general, you can download and install it with the following syntax:

curl https://raw.githubusercontent.com/creationix/nvm/v0.16.1/install.sh | sh

This will download the script and run it. It will install the software into a subdirectory of your home directory at ~/.nvm. It will also add the necessary lines to your ~/.profile file to use the file.

To gain access to the nvm functionality, you'll need to log out and log back in again, or you can source the ~/.profile file so that your current session knows about the changes:

source ~/.profile

Now that you have nvm installed, you can install isolated Node.js versions.

To find out the versions of Node.js that are available for installation, you can type:

nvm ls-remote
. . .

v0.11.10
v0.11.11
v0.11.12
v0.11.13
v0.11.14

As you can see, the newest version at the time of this writing is v0.11.14. You can install that by typing:

nvm install 0.11.14

Usually, nvm will switch to use the most recently installed version. You can explicitly tell nvm to use the version we just downloaded by typing:

nvm use 0.11.14

When you install Node.js using nvm, the executable is called node. You can see the version currently being used by the shell by typing:

node -v

The comeplete tutorial can be found here

Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106
  • 3
    This also fixed error above for me. It is much better for nodejs developer (in my view the whole developers, i am not one who work in nodejs). I did use it. – Ajeeb.K.P Jan 22 '15 at 07:42
  • 1
    I needed to soft link the nvm node to `usr/bin/node`. So ran `sudo ln -s /home/www/.nvm/v0.10.36/bin/node /usr/bin/node ` – Jason Kim Mar 09 '15 at 06:20
  • 2
    I have been working trying to figure this out for hours.. This is the only solution I found to work. Thank you. – w3bMak3r Mar 30 '15 at 02:31
  • 'benefits' is now a dead link – jaunt Dec 16 '17 at 18:23
20
  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);
    n=${n%/bin/node}; 
    chmod -R 755 $n/bin/*; 
    sudo cp -r $n/{bin,lib,share} /usr/local
    
mnille
  • 1,328
  • 4
  • 16
  • 20
Ritesh
  • 201
  • 2
  • 5
14
sudo apt-get --purge remove node
sudo apt-get --purge remove nodejs-legacy
sudo apt-get --purge remove nodejs

sudo apt-get install nodejs-legacy
source ~/.profile

Combined the accepted answer with source ~/.profile from the comment that has been folded and some clean up commands before. Most likely you will also need to sudo apt-get install npm after.

Devs love ZenUML
  • 11,344
  • 8
  • 53
  • 67
12

for me problem was solved by,

sudo apt-get remove node
sudo apt-get remove nodejs
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo ln -s /usr/bin/nodejs /usr/bin/node
alias node=nodejs
rm -r /usr/local/lib/python2.7/dist-packages/localstack/node_modules
npm install -g npm@latest || sudo npm install -g npm@latest
shrishinde
  • 3,219
  • 1
  • 20
  • 31
10

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>.

Ville
  • 4,088
  • 2
  • 37
  • 38
9

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.

Suzana
  • 4,251
  • 2
  • 28
  • 52
9

As other folks already mention, I will suggest not to use "sudo apt-get" to install node or any development library. You can download required version from https://nodejs.org/dist/v6.9.2/ and setup you own environment.

I will recommend tools like nvm and n, to manage you node version. It is very convenient to switch and work with these modules. https://github.com/creationix/nvm https://github.com/tj/n

Or write basic bash to download zip/tar, extract move folder and create a soft link. Whenever you need to update, just point the old soft link to new downloaded version. Like I have created for my own, you can refer: https://github.com/deepakshrma/NodeJs-4.0-Reference-Guide/blob/master/nodejs-installer.sh

#Go to home
cd ~
#run command
#New Script
wget https://raw.githubusercontent.com/deepakshrma/NodeJs-4.0-Reference-Guide/master/nodejs-installer.sh 
bash nodejs-installer.sh -v lts
#here -v or --version can be sepecific to 0.10.37 or it could be latest/lts 
#Examples
bash nodejs-installer.sh -v lts
bash nodejs-installer.sh -v latest
bash nodejs-installer.sh -v 4.4.2
xdeepakv
  • 7,835
  • 2
  • 22
  • 32
7

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

5

Your System is not able to detect the path node js binary.

1.which node

2.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.

frostcs
  • 353
  • 5
  • 10
4

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.

  • FYI - anyone using the above method, `nvm`, note that the nodejs-legacy package is v0.10.25. (or at least, that's what I saw when I tried it.) – JaredH May 28 '16 at 00:14
3

I fixed it unlinking /usr/sbin/node (which is linked to ax25-node package), then I have create a link to nodejs using this on command line

sudo ln -s /usr/bin/nodejs /usr/bin/node

Because package such as karma doesn't work with nodejs name, however changing the first line of karma script from node to nodejs, but I prefer resolve this issue once and for all

1

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
aaaidan
  • 7,093
  • 8
  • 66
  • 102
Jimmy
  • 2,165
  • 1
  • 17
  • 13
1

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

Vinayk93
  • 353
  • 1
  • 6
1

This is the your node is not properly install, first you need to uninstall the node then install again. To install the node this may help you http://array151.com/blog/nodejs-tutorial-and-set-up/

after that you can install the packages easily. To install the packages this may help you

http://array151.com/blog/npm-node-package-manager/

Dinesh Jain
  • 149
  • 11
0

you can create a link ln -s nodejs node in /usr/bin hope this solves your problem.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
Nitish Agarwal
  • 692
  • 1
  • 6
  • 16
0
node -v  // first check it's install or not
npm -v
sudo apt install npm
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash –
sudo apt-get install nodejs

then check

node -v or node –version
npm -v or npm –version

or you can remove package.lock json file / node_modules than run npm i

I hope it'll work fine

steps : https://www.geeksforgeeks.org/installation-of-node-js-on-linux/

Pradip Dhakal
  • 1,872
  • 1
  • 12
  • 29
0

Faced same issue, steps below worked for me.

Install curl on your system then run NVM installer script.

sudo apt install curl 
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash

Load the environment

source ~/.profile 

Install the supported version of Node.js.

nvm install 16.15.1

Confirm the installation

node -v
TrevorDeTutor
  • 683
  • 6
  • 11