how to set the default node version?? After restarting server - node.js

$ nvm ls:
.nvm
v0.11.12
v0.11.13
I have to keep hitting nvm use v.0.11.13 in every session:
.nvm
v0.11.12
-> v0.11.13
I've tried both the brew install, as well as the official installation script.

use this in the terminal.
$ nvm alias default v0.11.13
for more help go to https://github.com/creationix/nvm or write nvm help in terminal.

Try putting a .nvmrc file at the root of your project. It shall contain:
v0.11.13
Then, you could add this little snippet at the end of your ~/.bashrc (or whatever bash you're using):
# Use node version (nvm use)
autoload -U add-zsh-hook
load-nvmrc() {
if [[ -f .nvmrc && -r .nvmrc ]]; then
nvm use
elif [[ $(nvm version) != $(nvm version default) ]]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc
This should properly set the node version as soon as you enter a directory containing a proper .nvmrc file.

Related

nodenv install returns node-build: TMPDIR is set to a non-accessible location

when I try and run nodenv install x.x.x it returns an error
node-build: TMPDIR=/var/folders/r6/582734pd05vctn829j19qzbh0000gn/T is set to a non-accessible location
I have tried installing installing nodenv using brew install nodenv and git clone https://github.com/nodenv/nodenv.git ~/.nodenv and both return the same error.
tldr: delete your node modules before running yarn install
Someone has named a node module touch that conflicts with the linux command of the same name. It causes the node-build installer to fail when testing the temp directory.
# Check if TMPDIR is accessible and can hold executables.
tmp_executable="${TMP}/node-build-test.$$"
noexec=""
if mkdir -p "$TMP" && touch "$tmp_executable" 2>/dev/null; then
cat > "$tmp_executable" <<-EOF
#!${BASH}
exit 0
EOF
chmod +x "$tmp_executable"
else
echo "node-build: TMPDIR=$TMP is set to a non-accessible location" >&2
exit 1
fi

How to automatically activate specific node version when VS Code integrated terminal opens?

I have two projects with different version of node, which are managed with nvm:
Project 1: node v6.10.4
Project 2: node v8.9.4
On each project's root folder there is a file .nvmrc with its node version. When I open the integrated terminal on VS Code the default node version is selected (other than the previous two) and I need to type $ nvm use to change to the correct version of node each project uses. Is there a way to execute automatically $ nvm use after terminal opens, or another way to achieve my objective? Sometimes I open the terminal and forget to execute the command.
I'm using Ubuntu 16.04, VS Code 1.20.1 and nvm 0.33.8.
Thanks.
I've found a solution:
To work with nvm, I added in ~/.bashrc (from nvm instructions):
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
Now I've changed it to:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" --no-use # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
if [ -f ".nvmrc" ]; then
nvm use > /dev/null
else
nvm use default > /dev/null
fi
The --no-use option is to tell nvm "not load default node bin dir on PATH", because it will be done later on the last lines: if .nvmrc exists on the directory where the integrated terminal opens, the version inside .nvmrc will be loaded, default version otherwise.

Installing nvm on Elastic Beanstalk

I'm trying to install nvm on my Elastic Beanstalk instance because our rails application requires node 6.9.5, currently node 4 exists on the instance. I'm running the commands
01_node_install:
command: "sudo yum install make glibc-devel gcc patch openssl-devel c++"
02_node_install:
command: "curl https://raw.githubusercontent.com/creationix/nvm/v0.16.1/install.sh | sh"
03_node_install:
command: "source ~/.bash_profile"
04_node_install:
command: "nvm install 6.9.5"
05_node_install:
command: "nvm alias default 6.9.5"
I am getting an error of
=> Profile not found. Tried ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.
=> Create one of them and run this script again
OR
=> Append the following lines to the correct file yourself:
export NVM_DIR="/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
=> Close and reopen your terminal to start using nvm
What's strange to me is that it's saying the file ~/.bash_profile does not exist but I can ssh into the instance and see it. I tried to echo the lines into the file but got similar errors.
Any help would be appreciated!
I noticed that this question has gotten at least some viewers over the past bit. I was able to do this with the following commands.
000_dd:
command: echo “noswap”#dd if=/dev/zero of=/swapfile bs=1M count=3072
001_mkswap:
command: echo “noswap”#mkswap /swapfile
002_swapon:
command: echo “noswap”#swapon /swapfile
01-install-nvm:
command: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.6/install.sh | bash
02-setup-bashrc:
command: |
cat << EOF >> /etc/bashrc
export NVM_DIR="/.nvm"
[ -s "\$NVM_DIR/nvm.sh" ] && \. "\$NVM_DIR/nvm.sh" # This loads nvm
[ -s "\$NVM_DIR/bash_completion" ] && \. "\$NVM_DIR/bash_completion" # This loads nvm bash_completion
EOF
03-install-node:
command: source /etc/bashrc && nvm install 6.9.5
04-set-node-default:
command: source /etc/bashrc && nvm alias default 6.9.5
05-set-node-default:
command: source /etc/bashrc && ln -sf $(nvm which 6.9.5) /usr/bin/node
Probably some poorly named commands but I hope this helps some people!

Need to define node version on every new terminal window

I've installed node version manager (NVM) and with that installed node.js v0.10.26. However, when I open a new terminal window node, npm, etc. are unavailable until I define a node version like so: "nvm use 0.10".
I'm assuming this is a $PATH issue, but I'm not really sure. How do I configure nvm so that I don't need to define the node.js version on every new terminal window.
Here's my .bash_profile:
export PATH="/Applications/MAMP/bin:/Applications/MAMP/Library/bin:$(find /Applications/MAMP/bin/php -type d -name "php5.4*" | sort | tail -1)/bin:$PATH"
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
export PATH=/usr/local/bin:$PATH
[ -s "/Users/tylermoore/.nvm/nvm.sh" ] && . "/Users/tylermoore/.nvm/nvm.sh" # This loads nvm
As I suspected this was a $PATH issue. I added:
/Users/[username]/.nvm/v0.10.26/bin
to my $PATH and all is good in the world now.

Node Version Manager install - nvm command not found

I am trying to install NVM as per these instructions
I typed in this command in terminal:
$ curl https://raw.github.com/creationix/nvm/master/install.sh | sh
After running the install, I restart the terminal and attempt to install Node.js with this command:
$ nvm install 0.8
but I get the response:
-bash: nvm: command not found
I'm not sure what I am doing wrong here.
Additional Info--
I've been looking around for solutions from other posts and forums. I found another solution using
$ git clone git://github.com/creationix/nvm.git ~/.nvm
but this times out every time I attempt that.
I think you missed this step:
source ~/.nvm/nvm.sh
You can run this command on the bash OR you can put it in the file /.bashrc or ~/.profile or ~/.zshrc to automatically load it
https://github.com/creationix/nvm
Check your .bash_profile, .zshrc, or .profile file. You most likely had a problem during the installation.
You should have the following at the end of one of those files.
[[ -s $HOME/.nvm/nvm.sh ]] && . $HOME/.nvm/nvm.sh # This loads NVM
The . $HOME/.nvm/nvm.sh is the same as source $HOME/.nvm/nvm.sh
See: Sourcing a File
You can also check to see if you have a .nvm folder.
ls -a | grep .nvm
If you're missing that folder then the installation failed to run the git command. This could be due to being behind a proxy. Try running the following instead.
git clone http://github.com/creationix/nvm.git .nvm
This works for me:
Before installing nvm, run this in terminal: touch ~/.bash_profile
After, run this in terminal:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash
Important... - DO NOT forget to Restart your terminal OR use command source ~/.nvm/nvm.sh (this will refresh the available commands in your system path).
In the terminal, use command nvm --version and you should see the version
Quick answer
Figure out the following:
Which shell is your terminal using, type in: echo $0 to find out (normally works)
Which start-up file does that shell load when starting up (NOT login shell starting file, the normal shell starting file, there is a difference!)
Add source ~/.nvm/nvm.sh to that file (assuming that file exists at that location, it is the default install location)
Start a new terminal session
Profit?
Example
As you can see it states zsh and not bash.
To fix this I needed to add source ~/.nvm/nvm.sh to the ~/.zshrc file as when starting a new terminal my Deepin Terminal zsh reads ~/.zshrc and not bashs ~/.bashrc.
Why does this happen
This happens because when installing NVM it adds code to ~/.bashrc, as my terminal Deepin Terminal uses zsh and not bash it never reads ~/.bashrc and therefor never loads NVM.
In other words: this is NVMs fault.
More on zsh can be read on one of the answers here.
Quick answer for zsh users
curl raw.github.com/creationix/nvm/master/install.sh | zsh
source ~/.nvm/nvm.sh Add this line to ~/.bashrc, ~/.profile, or ~/.zshrc
All answers to this questions are useful. Especially the answer given by Travis helped me. For Mac OS X users I would like to provide some steps which will help them to work with the fresh installation of Node Version Manager a.k.a. nvm.
Installing & using nvm on Mac OS X
Here are the steps for fresh installation of nvm and using it without any issue:
Install homebrew from here.
Using homebrew install nvm
brew update
brew install nvm
Create .nvm directory at ~/.nvm location.
mkdir ~/.nvm
Now if you don't have .bash_profile file setup for OS X terminal then please create a .bash_profile at the root level:
nano ~/.bash_profile
Paste below code in the .bash_profile and press CTRL + O and press enter to save .bash_profile file. Press CTRL + X to exit from editor:
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
Now either quite (CMD + Q) the terminal or run below command to load .bash_profile settings:
source ~/.bash_profile
Now run nvm ls command to get the list of all installed nodejs versions.
In macOS, i had to source it using source ~/.nvm/nvm.sh command to fix this problem.
After that, add these lines
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
onto ~/.bash_profile so that nvm will be sourced automatically upon login.
Assuming that you have installed nvm using brew (I am on a mac), I mean something like brew install nvm, all you need to do is:
1. You should create NVM's working directory if it doesn't exist:
mkdir ~/.nvm
2. Add the following to ~/.zshrc or your desired shell configuration
file:
export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh" # This loads nvm
[ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
I faced a similar issue when installing nvm on a MacBook Pro.
I had installed nvm initially using brew:
brew install nvm
But when I run the command:
nvm --version
I get the error:
zsh nvm: command not found
Here's how I fixed it:
Installing nvm using brew is not enough to get it to work. You also need to do the following below;
Confirm that the source ~/.zshrc, ~/.bash_profile, ~/.profile, or ~/.bashrc file exists:
zsh: ~/.zshrc
bash: ~/.bashrc
ksh: ~/.profile
Else create it using:
touch ~/.zshrc
touch ~/.bashrc
touch ~/.profile
Next, run either of the commands below:
Note: You can check the referenced link below to get the updated commands.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
OR
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Note: Running either of the above commands downloads a script and runs it. The script clones the nvm repository to ~/.nvm, and attempts to add the source lines from the snippet below to the correct profile file (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc)
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.
You can check the referenced link below to get the updated script.
Reference: Installing and Updating NVM
Same problem encountered. You have to source shell file as well as change nvm's default version.
First, source ~/.nvm/nvm.sh.
Second, change nvm's default version.
nvm alias default 16.14.2
If you don't change nvm's default version, you will find the node version stay the same when you switch to another iTerm tab.
OSX 10.15.0 Catalina (released November 2019) changed the default shell to zsh.
The default shell was previously bash.
The installation command given on the nvm GitHub page needs to be tweaked to include "zsh" at the end.
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | zsh
Note: you might need to ensure the .rc file for zsh is present beforehand:
touch ~/.zsrhrc
I have the same problem and what saved my life is the sentence "you may have to add to more than one of your "~/.bashrc, ~/.profile, or ~/.zshrc files". the following lines were in my .bashrc only, I added it to files ".bash_profile" and ".profile" and worked for me .
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
First add following lines in ~/.bashrc file
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
then open terminal and source the nvm.sh script
source ~/.nvm/nvm.sh
For MacOS;
Run on Terminal >
open ~/.bash_profile
Paste all of this=
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
For Mac OS:
Open Terminal
Run touch ~/.bash_profile
Run vi ~/.bash_profile
Type source ~/.nvm/nvm.sh
Press Shift + Esc and type wq and press enter
Done.
For Mac OS:
Open Terminal
Check if you have .profile file with this command:
ls -a ~/
If you havnt this file just create one:
touch ~/.profile
Add this command to .profile file :
source ~/.nvm/nvm.sh
Press Shift + Esc and type wq and press enter
Not directly connected to the question, but there is a similar problem that may happen, take a look at this question: Can't execute nvm from new bash
Here's my answer on that post, just for the reference:
If you are running from a new bash instance, and you HAVE the initialization code at your ~/.bashrc, ~/.bash_profile, etc, then you need to check this initialization file for conditionals.
On Ubuntu 14, there is a:
case $- in
*i*) ;;
*) return;;
esac
At line 6, that will halt it's execution if bash is not being ran with the "-i" (interactive) flag. So you would need to run:
bash -i
Also, at the end of the file, there is a
[ -z "$PS1" ] && return
That will halt it's execution if not being ran with $PS1 set (like on a remote ssh session).
If you do not wish to add any env vars or flags, you will need to remove those conditionals from your initialization file.
Hope that's helpful.
If you are using OS X, you might have to create your .bash_profile file before running the installation command. That did it for me.
Create the profile file
touch ~/.bash_profile
Re-run the install and you'll see a relevant line in the output this time.
=> Appending source string to /Users/{username}/.bash_profile
Reload your profile (or close/re-open the Terminal window).
. ~/.bash_profile
Add the following lines to the files ~/.bashrc and ~/.bash_profile :
# NVM changes
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
and restart the terminal or do source ~/.bashrc or source ~/.bash_profile. If you need command completion for nvm then also add the line:
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
Along with the above lines to ~/.bashrc and ~/.bash_profile.
For the issue was fixed when I moved
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
to the end of .zshrc
Over time, nvm (from homebrew) changed its post-installation instructions.
I was getting nvm_find_nvmrc not found on a new computer, until I replaced
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
with
[ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh" # This loads nvm
[ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
which properly found the installation location.
I had fixed this problem.
touch ~/.bash_profile
open ~/.bash_profile
pasteexport NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
In Windows 8.1 x64 same happened with me, and received the following message.
nvm install 8.3.0 bash: nvm: command not found windows
So, follow or verify below following steps-
first install coreybutler/nvm-windows from github.com. Currently available latest release 1.1.5 nvm-setup.zip, later extracted the setup nvm-setup.exe and install as following locations:
NVM_HOME : C:\Users\Administrator\nvm
NVM_SYMLINK : C:\Program Files\nodejs
and meanwhile setup will manage the environment variable to Path as above said for you.
Now run Git Bash as Administrator and then.
$ nvm install 8.3.0 all
Downloading node.js version 8.3.0 (64-bit)...
Complete
Creating C:\Users\Administrator\nvm\temp
Downloading npm version 5.3.0... Complete
Installing npm v5.3.0...
Installation complete. If you want to use this version, type
nvm use 8.3.0
$ nvm use 8.3.0
Now using node v8.3.0 (64-bit)
here run your command without using prefix $, it is just shown here to determine it as a command line and now we will verify the nvm version.
$ nvm --version
Running version 1.1.5.
Usage:
-----------------------
if you have problem using nvm to install node, you can see this list of available nodejs releases here https://nodejs.org/download/release/ and choose the correct installer as per your requirement version equal or higher than v6.3.0 directly.
Had the same problem, but this worked for me:
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.
more info here: Troubleshooting on macOS
After spending more than 3 hours testing some of the solutions above I came across an answer that was working for me.
I am under Ubuntu 20.04.3 LTS and with a fresh install added the official command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh |
bash
The problem was that my version of curl was obtained via snap. Removing it and re-installing it as suggested by this answer fixed the issue:
sudo snap remove curl
sudo apt install curl
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh
| bash
nvm install node
The nvm install script by default adds initialization code to your $HOME/.profile, which is only loaded by a login shell (in a desktop environment you may never see a login shell).
The nvm command in your login shell is not propagated to sub-shells (like console windows and IDE terminals after you log in). This snippet in your $HOME/.bashrc will only load nvm if it is an interactive shell and has not been loaded already
# if nvm dir is not set and the standard nvm directory exists
if [ -z "$NVM_DIR" -a -d "$HOME/.nvm" ] ; then
# set nvm dir
export NVM_DIR="$HOME/.nvm"
fi
# if nvm dir is set and this shell is interactive
if [ -d "$NVM_DIR" -a -n "$PS1" ] ; then
# if nvm command is not defined
if ! type -t nvm >/dev/null ; then
# set it
source "$NVM_DIR/nvm.sh"
fi
fi
Putting this in your $HOME/.bashrc file will fix the missing nvm problem in interactive bash shells, even from a gui, and even if nvm is installed in a non-standard location.
For me this worked.
First check that the file .bashrc has following line
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
Now bashrc is loaded for each terminal session. Thus restart the terminal so that it is loaded again and you should be good to go.
On Debian, as well as adding the below lines to my .bash_profile as one of the above answers said. I also had to open up my terminal preferences (Edit -> Profile Preferences -> Command) and enable 'Run command as a login shell' to get it to work.
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
Edit: For those on Mac be aware that macOS doesn't read .bashrc on Terminal start, so using .bash_profile is preferable. See Here.
For my case, it because I use fish. if I not start fish, just type nvm will no error now.
Something to be aware of, which tripped me up for an hour, is that nvm is "Implemented as a POSIX-compliant function", as it states at the top of the nvm.sh script file. This means there's no nvm file in your path, and thus you can't verify that nvm is present using the which command. Instead, use nvm --version, and verify that you get expected output.
malderete-ltm:~ malderete$ nvm --version
0.38.0
malderete-ltm:~ malderete$ which nvm
malderete-ltm:~ malderete$ which node
malderete-ltm:~ malderete$ nvm install --lts
Installing latest LTS version.
Downloading and installing node v14.17.5...
Downloading https://nodejs.org/dist/v14.17.5/node-v14.17.5-darwin-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with shasum -a 256
Checksums matched!
Now using node v14.17.5 (npm v6.14.14)
Creating default alias: default -> lts/* (-> v14.17.5)
malderete-ltm:~ malderete$ nvm use --lts
Now using node v14.17.5 (npm v6.14.14)
malderete-ltm:~ malderete$ which node
/Users/malderete/.nvm/versions/node/v14.17.5/bin/node
(As someone who is only semi-command line literate, I lean on the which command by muscle memory. Using nvm --version is in the instructions, but if you're an idiot like me, you might miss/skip over that part, and use which nvm by reflex, and think that nvm isn't installed.)

Resources