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

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

When I was trying to install a module using npm today, it returned an error stating that my nodejs version is old and it needs to be updated. Then I checked the node version and it was really old, and noticed I installed it using the Ubuntu repos (which doesn't get latest updates.)

So I installed the latest LTS version of node using the Node Version Manager (NVM) and noticed that I had two versions of node running after this upgrade. ie:

$ node -v

$ nodejs -v

This caused problems when using the NPM because it was referring to old nodejs version. So I removed it with the command

sudo apt-get remove nodejs
Now whenever I ran the npm install command, it failed stating:

/usr/bin/env: node: No such file or directory
So to confirm if I'm using the right node binary, I ran the command which node and it showed the right node binary path inside the nvm directory inside my home directory.

Although it showed the correct path for node, npm failed because its referring to node inside my /usr/bin path.

Then I ran the following command to see what I have in my /usr/bin/ directory for node and found that it was linked to /usr/bin/nodejs (which I uninstalled earlier).

So to fix the issue, I removed the /usr/bin/node symlink and added a new symlink to point to the right node binary path.

sudo rm /usr/bin/node
sudo ln -s /home/nimeshka/.nvm/versions/node/v8.9.3/bin/node /usr/bin/node

Instead of creating the symlink with this full path, you can enter

sudo ln -s "$(which node)" /usr/bin/node
$(which node) will be excecuted when you run the command and will be replaced with your path to node :)

I hope this post will help someone to fix this error. Please don't forget to leave a comment if this helped you, or if I have missed anything.