Skip to content

Instantly share code, notes, and snippets.

@MeLlamoPablo
Created February 1, 2017 11:34
Show Gist options
  • Save MeLlamoPablo/0abcc150c10911047fd9e5041b105c34 to your computer and use it in GitHub Desktop.
Save MeLlamoPablo/0abcc150c10911047fd9e5041b105c34 to your computer and use it in GitHub Desktop.
Creates a symlink to /usr/bin/node after using nvm
@wmentzel
Copy link

If you don't want to have to update the symlink after setting another node version via nvm, just run:

sudo ln -s node /usr/bin/

/usr/bin/node will point to node then instead of a specific version which would happen when using $(which node). Of course you should do the same for npm.

@RichardTMiles
Copy link

RichardTMiles commented May 21, 2022

A little more robust...

https://stackoverflow.com/questions/5767062/how-to-check-if-a-symlink-exists
https://linuxcommand.org/lc3_man_pages/seth.html

Thanks for the post, I googled if nvm had a flag and this was the first result :) good stuff

#!/bin/bash

set -eEB

if [ -e /usr/bin/node ]; then

  sudo rm -f /usr/bin/node

fi

if [ -e /usr/bin/npm ]; then

  sudo rm -f /usr/bin/npm

fi

sudo ln -s "$(which node)" /usr/bin/

sudo ln -s "$(which npm)" /usr/bin/

@MeLlamoPablo
Copy link
Author

If you don't want to have to update the symlink after setting another node version via nvm, just run:

sudo ln -s node /usr/bin/

/usr/bin/node will point to node then instead of a specific version which would happen when using $(which node). Of course you should do the same for npm.

Correct me if I'm wrong, but that only works for if node is already in $PATH which defeats the point of this script, which is to force node from nvm into $PATH for applications that don't support it (because most distros/apps by default will have /usr/bin in $PATH).

To be clear, I think this is a bad practice. Each user should have their own nvm installation and $NVM_DIR in $PATH.

I only originally did this because NVM was causing some frictions with WebStorm, as I couldn't make WebStorm read NVM_DIR. But nowadays WebStorm seems to support NVM natively and I personally don't have any use cases for this script.

@ClintEsteMadera
Copy link

sudo ln -s node /usr/bin/

this is the best option, IMHO. Modify once and forget forever

@rjmacarthy
Copy link

Nice one!

@fandredev
Copy link

thanks for this

@ramphy
Copy link

ramphy commented Sep 14, 2023

thank youu!

@geuis
Copy link

geuis commented Nov 4, 2023

The latest one linked sudo ln -s node /usr/bin can lead to recursive symlinks. However, using the original 4 liner at the beginning of the gist works reliably.

@adm-bome
Copy link

adm-bome commented May 27, 2024

SystemWide:

Create a userspace independend script:

sudo tee /usr/bin/link-current-nvm-version-to-system << FILE_CONTENT
#!/bin/bash

NVM_CURRENT_BIN="\$HOME/.nvm/current/bin"

if [ -e /usr/bin/node ]; then
  sudo rm -f /usr/bin/node
fi

if [ -e /usr/bin/npm ]; then
  sudo rm -f /usr/bin/npm
fi

sudo ln -s "\$NVM_CURRENT_BIN/node" /usr/bin/
sudo ln -s "\$NVM_CURRENT_BIN/npm" /usr/bin/
FILE_CONTENT

Create a sudoers file specific to linking: node and nvm

sudo tee /etc/sudoers.d/nvm-linking << FILE_CONTENT
%sudo ALL=(ALL:ALL) NOPASSWD: /usr/bin/rm -f /usr/bin/npm
%sudo ALL=(ALL:ALL) NOPASSWD: /usr/bin/rm -f /usr/bin/node
%sudo ALL=(ALL:ALL) NOPASSWD: /usr/bin/ln -s /home/*/npm /usr/bin/
%sudo ALL=(ALL:ALL) NOPASSWD: /usr/bin/ln -s /home/*/node /usr/bin/
FILE_CONTENT

Every User:

Add to User space: ~/.bashrc or ~/.zshrc

export NVM_SYMLINK_CURRENT=true
export NVM_DIR="$HOME/.nvm"
export NVM_CURRENT_BIN="$NVM_DIR/current/bin"

if [[ -s "$NVM_DIR/nvm.sh" ]]; then
    source "$NVM_DIR/nvm.sh"
fi

Create an autostart file when user logs into the system (repeat for every user)

tee ~/.config/autostart/link-current-nvm-to-system.desktop << FILE_CONTENT
[Desktop Entry]
Type=Application
Terminal=false
Name=Link Current NVM to System
Exec=/usr/sbin/link-current-nvm-version-to-system
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
FILE_CONTENT

Now you can switch node/npm versions as you like and the local node/npm wil always point to the Current active version

nvm install --lts
nvm ls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment