how to make nvm automatically sourced upon login - node.js

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

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.

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! :)

For Mac at least, open your .profile file, and add this line in
[[ -s $HOME/.nvm/nvm.sh ]] && source "$HOME/.nvm/nvm.sh"
# Load NVM into a shell session *as a function*
Done, bingo bango, GG.

Please use the official website carefully as I used
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | zsh from https://github.com/nvm-sh/nvm and it automatically does my work from installing to load automatically setup.

Related

Node doesn't start without source ~/.bash_profile command

I have to use source ~/.bash_profile for node command to work on my terminal. All the node commands work just fine after using source ~/.bash_profile. I used nvm to install Node LTS(v14.18.0).
My .bash_profile
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
Is there any way to fix my node installation?
MacOS uses zsh as the default shell and not bash. You will need to create a ~/.zshrc file and then run the install script of nvm again.
You can probably skip the rerunning the install script by manually inserting in .zshrc file the following:
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
"Since macOS 10.15, the default shell is zsh and nvm will look for .zshrc to update, none is installed by default. Create one with touch ~/.zshrc and run the install script again." - from nvm docs on github.
https://github.com/nvm-sh/nvm#troubleshooting-on-macos
I was facing the same issue and the above solution didn't work for me.
Finally the solution that did work for me was adding these lines to ~/.zshrc file.
source ~/.nvm/nvm.sh

bash changing directory when started

I have both Bash on Ubuntu on Windows and Cygwin bash installed on my machine, and both are setup to have the same ~ folder (via /mnt/c/source and /cygdrive/c/source respectively).
When I start Ubuntu's bash prompt via bash --login -i (or just bash --login) from any directory, I get a prompt running from within that directory; however, when I start Cygwin's bash via the same command, the current directory is overridden, and the prompt is always at ~. See the screenshots for a simple example.
My user directory's .bashrc and .bash_profile are of course the same, as both are using the same user directory. I've looked into Cygwin's /etc/bash.bashrc and there doesn't seem to be anything there to change my current directory, and there aren't any other relevant files in /etc.
What could be causing Cygwin's bash to change directory?
you just add a command "cd /dir_you_want" at the bottom of ~/.bashrc in cygwin
I've figured it out, so in case anyone runs into the same issue:
There's one file I neglected to look into, because I didn't know it exists, /etc/profile. In Cygwin, by default it has the following section in it:
# Make sure we start in home unless invoked by CHERE
if [ ! -z "${CHERE_INVOKING}" ]; then
unset CHERE_INVOKING
else
cd "${HOME}" || echo "WARNING: Failed attempt to cd into ${HOME}!"
fi
Disabling that solves the issue of course.

Closing the terminal doesnt remember my .profile changes for nvm/npm

I installed nvm/npm using this instruction
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-an-ubuntu-14-04-server (How To Install Using NVM), but everytime I close my terminal, it seems to forget all the settings and I have to do the command:
source ~/.profile then select the npm version to make it run again. How can I keep my settings permanent, or at least for the duration of my logged in session? Thanks! (linuxmint 17)
Sourcing ~/.profile
~/.profile is typically invoked from a login shell. See http://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files
Invoked as an interactive login shell, or with --login
When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
Opening a terminal does not typically invoke a login shell. Have you tried rebooting/relogging? Additionally if either ~/.bash_profile or ~/.bash_login exist, then bash won't source ~/.profile. There are two common ways to fix this:
Option 1
Move commands from the ~/.profile file to ~/.bash_profile
Option 2
Source ~/.profile from ~/.bash_profile:
[[ -f ~/.profile ]] && . ~/.profile
Default Node version
You can configure a default node version by aliasing it to default
To set a default Node version to be used in any new shell, use the alias 'default':
nvm alias default node
See https://github.com/creationix/nvm

Bash complains "have not a command" when login

I have a file named "upstart" in /etc/bash_completion.d/ with the following content:
# bash-completion for the upstart event-based init replacement
# (http://upstart.ubuntu.com / https://launchpad.net/upstart)
#
# We don't provide completion for 'init' itself for obvious reasons.
have initctl &&
_upstart_jobs()
{
initctl list|awk '{print $1}'|sort -u
} &&
The confusing part is the line have initctl &&, I have configured bash to source
all files in /etc/bash_completion.d/ and every time I login it complains
that command have cannot be found. What is that line for?
Short Answer: maybe you should install bash-completion package.
In general, you don't need to manually source the files in /etc/bash_completion.d/.
It is automatically imported by bash_completion script.
bash_completion script is called by /etc/bash.bashrc (at least for Ubuntu and Arch Linux), which would be called by default.
In Ubuntu, bash_completion script is in bash-completion package and placed in /etc/bash_completion
In Arch Linux, bash_completion script is in bash-completion package and placed in /usr/share/bash-completion/bash_completion
Therefore, installing bash-completion script would help if you are using Ubuntu or Arch Linux.

Node.js stops working when I log out

I logged in as root to my CentOS 5/cPanel server and I typed the following:
cd /usr/local/bin/
git clone --depth 1 http://github.com/joyent/node.git
cd node
git checkout origin/v0.4 # optional. Note that master is unstable.
export JOBS=2 # optional, sets number of parallel commands.
mkdir ~/local
./configure --prefix=$HOME/local/node
make
make install
echo 'export PATH=$HOME/local/node/bin:$PATH' >> ~/.profile
source ~/.profile
It seems to be working fine until I log out from the server and log back in it's as it wasn't installed:
[~]# node test.js
-bash: node: command not found
If I type: source ~/.profile it starts working again until I log out.
Please help. Thanks.
EDIT:
This is the content of my .bash_profile, how should I change it?
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
unset USERNAME
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
Maybe you have a .bash_profile and it's being used instead?

Resources