109

I have installed nodejs using:

apt-get install nodejs

Then i have installed npm using:

apt-get install npm

And then i have installed forever using:

npm install forever -g

Now i go to my project /var/www/myproject

and attempt to run forever start server.js

then i get the following message:

/usr/bin/env: node: No such file or directory

Can anyone tell me whats going on?

chedabob
  • 5,835
  • 2
  • 24
  • 44
Marc Rasmussen
  • 19,771
  • 79
  • 203
  • 364
  • Possible duplicate of [Cannot install NodeJs: /usr/bin/env: node: No such file or directory](http://stackoverflow.com/questions/26320901/cannot-install-nodejs-usr-bin-env-node-no-such-file-or-directory) – marcanuy Nov 15 '16 at 14:52
  • 1
    I get this bug and I am using Node Version Manager. – munchschair Jul 19 '17 at 13:11

3 Answers3

274

EDIT: As of December 2018, this is no longer the correct way. See the other two answers.

You need to symlink the nodejs executable to node sudo ln -s "$(which nodejs)" /usr/local/bin/node The reason for this is that when you do "apt-get install node", it installs an unrelated package, so they had to choose a different name so it wouldn't conflict

chedabob
  • 5,835
  • 2
  • 24
  • 44
  • 4
    what is $(which nodejs)" is the code you paste something i should just copy paste or? – Marc Rasmussen May 16 '15 at 21:54
  • 11
    "$(which nodejs)" gets the path of the nodejs executable. When you put something inside of "$()" it gets executed and then inserted into the containing command – chedabob May 16 '15 at 21:57
  • 2
    @chedabob I have faced the same problem but I had not yet resolved it when I am running your command at that time I got the ln: failed to create symbolic link ‘/usr/bin/node’: File exists – DASADIYA CHAITANYA Oct 22 '15 at 13:22
  • @dasadiya-chaitanya In that case make sure that the existing file is pointing to a correct node executable by running the following. ls -lart /usr/bin/node. If the symlink is not pointing to the correct binary, in that case delete it. And re-run the following: sudo ln -s "$(which nodejs)" /usr/bin/node – brownmamba Feb 02 '16 at 00:30
  • @brownmamba I have fixed it from my side but not going with your way but finally thanks – DASADIYA CHAITANYA Feb 02 '16 at 04:18
  • 4
    On Debian and Ubuntu, there is a package `nodejs-legacy` providing the symbolic link. You're not supposed to do it manually. `apt-get install nodejs-legacy` is the correct way to fix the problem, see my answer below. – Clément Schreiner Mar 09 '16 at 08:54
44

While the accepted answer fixes the problem, the correct way to do that, at least with Debian Jessie and forward and Ubuntu 14.4 and forward1 is to install nodejs-legacy:

apt-get install nodejs-legacy

The reason is that Debian already had a package (node) providing /usr/bin/node, and the nodejs node binary had to be installed into /usr/bin/nodejs.

The nodejs-legacy package provides a symbolic link from /usr/bin/nodejs to /usr/bin/node (and conflicts with the node package).

Source: [CTTE #614907] Resolution of node/nodejs conflict and Debian bug #614907: node: name conflicts with node.js interpreter

Clément Schreiner
  • 1,087
  • 1
  • 8
  • 16
  • This is the correct way on Debian/Ubuntu +1, great context for those not familiar with the OS. – akahunahi Feb 14 '18 at 00:24
  • 1
    this broke npm completely `SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode`, even uninstalling legacy and reinstalling npm throws the same error. – Tcll Aug 22 '20 at 16:57
  • continuing my last comment, [this answer](https://stackoverflow.com/a/43125173/2131849) fixed what the current answer broke, and I was able to continue with the answer provided by @Shantanu – Tcll Aug 22 '20 at 17:20
  • do not do this please, it will break your npm install – Sebastian Dec 01 '20 at 10:25
22

It's better if you update to the latest node version

  1. sudo npm cache clean -f
  2. sudo npm install -g n
  3. sudo n stable
Shantanu
  • 2,206
  • 18
  • 16