Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make nvm automatically sourced upon login

Tags:

node.js

nvm

I am using nvm to manage the node version.

I like to make nvm sourced upon login, so I dont have to do it manually everytime I login.

in my user home directory, there's a .bashrc file. I appended following two lines to the end of the file. then restart my mac os. after I login, nvm is not sourced. I have to manually run them again. coudln't figure out whats wrong. please help.

. ~/nvm/nvm.sh

nvm use 0.8.20
like image 724
qinking126 Avatar asked Dec 21 '22 09:12

qinking126


2 Answers

You need to put into your .bash_profile this line

[[ -s $HOME/.nvm/nvm.sh ]] && . $HOME/.nvm/nvm.sh

Source the file (source ~/.bash_profile) or reopen the shell and then in the shell execute:

$ nvm alias default <version>

That would load node in any new shell you open.

like image 111
Maldo Avatar answered Dec 24 '22 00:12

Maldo


Using .nvmrc file

It turns out that creating a .nvmrc file in your home directory is enough. It is loaded automatically when you open the terminal.

To create the .nvmrc file with the version we want to run (0.12.2) do this:

echo '0.12.2' > ~/.nvmrc

If that for some reason doesn't work, force load the nvm from your .bashrc

But only if you're running in interactive mode.

Add the command to run nvm use 0.12.2 (or any other version you'd like to run) if the shell is launched in interactive mode to the end of .bashrc.

echo '[[ $- == *i* ]] && nvm use 0.12.2' >> ~/.bashrc

Boom, done! :)

like image 39
nana Avatar answered Dec 24 '22 02:12

nana