zsh: command not found: npm - node.js

I have just installed Node and Yarn using the following commands:
brew install node
brew install yarn --without-node
node -v
v11.8.0
-a node
node is /usr/local/bin/node
yarn -v
1.13.0
type -a yarn
yarn is /usr/local/bin/yarn
However, if I write in terminal npm is says "zsh: command not found: npm"
In my: ~/.zshrc I have:
ZSH=$HOME/.oh-my-zsh
# You can change the theme with another one:
# https://github.com/robbyrussell/oh-my-zsh/wiki/themes
ZSH_THEME="robbyrussell"
# Useful plugins for Rails development with Sublime Text
plugins=(gitfast last-working-dir common-aliases sublime zsh-syntax-highlighting history-substring-search)
# Prevent Homebrew from reporting - https://github.com/Homebrew/brew/blob/master/share/doc/homebrew/Analytics.md
export HOMEBREW_NO_ANALYTICS=1
# Actually load Oh-My-Zsh
source "${ZSH}/oh-my-zsh.sh"
unalias rm # No interactive rm by default (brought by plugins/common-aliases)
# Load rbenv if installed
export PATH="${HOME}/.rbenv/bin:${PATH}"
type -a rbenv > /dev/null && eval "$(rbenv init -)"
# Rails and Ruby uses the local `bin` folder to store binstubs.
# So instead of running `bin/rails` like the doc says, just run `rails`
# Same for `./node_modules/.bin` and nodejs
export PATH="./bin:./node_modules/.bin:${PATH}:/usr/local/sbin"
# Store your own aliases in the ~/.aliases file and load the here.
[[ -f "$HOME/.aliases" ]] && source "$HOME/.aliases"
# Encoding stuff for the terminal
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
I tried to uninstall and reinstall Node and Yarn using this guide: How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)
But I still get the same error.

It worked for me when I added source ~/.bash_profile at the beginning of my ~/.zshrc and I had to also remove
test -e "${HOME}/.iterm2_shell_integration.bash" && source "${HOME}/.iterm2_shell_integration.bash"
in ~/.bash_profile. This was the shell integration I accidentally installed from iTerm2.

Solved it by uninstalling Node completely and installing it via the website (not via terminal)

Solved it by running brew uninstall node and downloading the latest node installing .pkg from their website.

solved by selecting a node version using nvm.
nvm ls
then
nvm use _version_code

Related

Installed the wrong package

I was installing some packages in Linux Ubuntu to begin code some projects in Node.js and react, but I did not follow the instructions on yarn's website, passed on shell only the command line sudo apt-get install yarn. Now, I got a wrong version installed and I can't install the right version. I also can't uninstall this wrong package. I tried everything I could think. Somebody who passed by it and got resolve it could help me?
Try sudo apt-get remove yarn && sudo apt-get purge yarn
First you need to check if your required version is installed or not by running this command in your terminal:
yarn --version
Now if the version is not installed you can run this command to remove and to install new version of it.
sudo apt-get remove yarn && sudo apt-get purge yarn
The root cause of your problem is using sudo to install node/yarn. Use the NVM package to install a node/yarn under your home folder. NVM is a convenient way to work with multiple versions of node and will help you with your yarn issue.
You can use nvm - install that and you can then use any versions of node. In fact you can install more than one version of node.
First download the NVM installation script using CURL as follows
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
After downloading this script, run the script using bash as given below
$ bash install_nvm.sh
Check the installed NVM version as given below
$ nvm --version
Install any particular node version using following nvm command:
$ nvm install 10.19 # will install node 10.19.0
To use particular node version use
$ nvm use 10.15 # it will use node 10.15.0
To list out all versions of node available to you
$ nvm ls
Finally after choosing the right version of node preferred by you, you can install yarn.
curl -o- -L https://yarnpkg.com/install.sh | bash
Test that Yarn is installed by running the below command -
yarn --version
With this method, you can avoid sudo issues.
If Yarn is not found in your PATH, follow these steps to add it and allow it to be run from anywhere.
Note: your profile may be in your .profile, .bash_profile, .bashrc, .zshrc, etc.
Add this to your profile: export PATH="$PATH:/opt/yarn-[version]/bin" (the path may vary depending on where you extracted Yarn to)
In the terminal, log in and log out for the changes to take effect

Can't use nvm from bash script

I am trying to write a shell script to automate my dev environment set-up (install python, nvm, node, mongo etc...). I am using nvm to install Node. It tells you to close and reopen your terminal to start using the nmv command. I tried to source .bashrc and .profile to make the command available right away so I can continue running the script with nvm install, but it doesn't work.
Here is the segment of my script related to installing NVM / Node:
#install nvm and latest node version
# sourcing profile and bashrc is not working here. nvm does not execute the next two lines to install node.
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.30.2/install.sh | bash
source ~/.profile
source ~/.bashrc
nvm install 5.0
nvm alias default node
I get these messages, but please note that I've already run the script and NVM / Node are already installed and working. I can also use nvm and node in the same terminal I run the script from after it completes. It just doesn't work in the script.
=> Downloading nvm from git to '/home/myDir/.nvm'
=> fatal: destination path '/home/myDir/.nvm' already exists and is not an empty directory.
fatal: Not a git repository (or any of the parent directories): .git
fatal: Not a git repository (or any of the parent directories): .git
=> Source string already in /home/myDir/.bashrc
=> Close and reopen your terminal to start using nvm
./install-programs.sh: line 27: nvm: command not found
./install-programs.sh: line 28: nvm: command not found
if you have nvm running on the main shell, you just need to add:
export NVM_DIR=$HOME/.nvm;
source $NVM_DIR/nvm.sh;
in your script
Here is what worked for me.
First install nvm (once and separately) using SSH or console:
$ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.30.2/install.sh | bash
Then in your script, loading your profile as follows:
. ~/.nvm/nvm.sh
. ~/.profile
. ~/.bashrc
And with some luck nvm should become available inside the script.
nvm install 4.4.2
Tada!
Just put this on top of your script:
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
Worked like a charm here.
This script works fine for me:
#!/usr/bin/env bash
if [ ! -d ~/.nvm ]; then
curl https://raw.githubusercontent.com/creationix/nvm/v0.11.1/install.sh | bash
source ~/.nvm/nvm.sh
source ~/.profile
source ~/.bashrc
nvm install 5.0
npm install
npm run front
fi
Nowadays, you can simply do this:
env NODE_VERSION=<dd> /home/<user>/.nvm/nvm-exec npm run front
Simply sourcing nvm.sh didn't work for me (from a systemd .service file), the PATH didn't include ~/.nvm...
Credit where credit is due: https://gist.github.com/joepie91/73ce30dd258296bd24af23e9c5f761aa#gistcomment-2215867

What is the recommended way to install Node.js, nvm and npm on MacOS X?

I am trying to use Homebrew as much as possible. What's the recommended way to install Node.js, nvm and npm on MacOS X?
Using homebrew install nvm:
brew update
brew install nvm
source $(brew --prefix nvm)/nvm.sh
Add the last command to the .profile, .bashrc or .zshrc file to not run it again on every terminal start. So for example to add it to the .profile run:
echo "source $(brew --prefix nvm)/nvm.sh" >> ~/.profile
If you have trouble with installing nvm using brew you can install it manually (see here)
Using nvm install node or iojs (you can install any version you want):
nvm install 0.10
# or
nvm install iojs-1.2.0
npm is shipping with node (or iojs), so it will be available after installing node (or iojs). You may want to upgrade it to the latest version:
$ npm install -g npm#latest
UPD Previous version was npm update -g npm. Thanks to #Metallica for pointing to the correct way (look at the comment bellow).
Using npm install ionic:
npm install -g ionic
What about ngCordova: you can install it using npm or bower. I don't know what variant is more fit for you, it depends on the package manager you want to use for the client side. So I'll describe them both:
Using npm: Go to your project folder and install ng-cordova in it:
npm install --save ng-cordova
Using bower: Install bower:
npm install -g bower
And then go to your project folder and install ngCordova in it:
bower install --save ngCordova
PS
Some commands may require superuser privilege
Short variant of npm install some_module is npm i some_module
Use nvm to install Node.js, not Homebrew
In most of the answers, the recommended way to install nvm is to use Homebrew.
Don't do this.
On nvm's Github Readme is clearly says:
Homebrew installation is not supported. If you have issues with homebrew-installed nvm, please brew uninstall it, and install it using the instructions below, before filing an issue.
Use the following method instead
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
The script clones the nvm repository to ~/.nvm and adds the source line to your profile (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).
And then use nvm to install node. For example to install latest LTS version do:
nvm install 16
Clean and hassle free. It will set this as your default Node.js version as well so you should be all set.
I'm using n (Node version management)
You can install it in two ways
brew install n
or
npm install -g n
You can switch between different version of node and io. Here's an example from my current env when I call n without params:
$ n
io/3.3.1
node/0.12.7
node/4.0.0
node/5.0.0
ο node/5.10.1
I'm super late to this but I didn't like the other answers
Installing Homebrew
For brew run
"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Installing node & npm
You SHOULD NOT use brew to install node and npm.
I've seen a few places suggested that you should use Homebrew to install Node (like alexpods answer and in this Team Treehouse blog Post) but installing this way you're more prone to run into issues as npm and brew are both package managers and you should have a package manager manage another package manager this leads to problems, like this bug offical npm issues Error: Refusing to delete: /usr/local/bin/npm or this Can't uninstall npm module on OSX
You can read more on the topic in DanHerbert's post Fixing npm On Mac OS X for Homebrew Users, where he goes on to say
Also, using the Homebrew installation of npm will require you to use sudo when installing global packages. Since one of the core ideas behind Homebrew is that apps can be installed without giving them root access, this is a bad idea.
For Everything else
I'd use npm; but you really should just follow the install instruction for each modules following the directions on there website as they will be more aware of any issue or bug they have than anyone else
If you have previously installed node using brew, then you will have a bunch of extra files that you should clean up before installing node "the right way". Plus, I had to add a few settings to my startup script to make things work smoothly.
I wrote a script to make this easy.
# filename: install-nvm-npm-node
# author: Lex Sheehan
# purpose: To cleanly install NVM, NODE and NPM
# dependencies: brew
NOW=$(date +%x\ %H:%M:%S)
CR=$'\n'
REV=$(tput rev)
OFF=$(tput sgr0)
BACKUP_DIR=$HOME/backups/nvm-npm-bower-caches/$NOW
MY_NAME=$(basename $0)
NODE_VER_TO_INSTALL=$1
if [ "$NODE_VER_TO_INSTALL" == "" ]; then
NODE_VER_TO_INSTALL=v0.12.2
fi
if [ "`echo "$NODE_VER_TO_INSTALL" | cut -c1-1`" != "v" ]; then
echo """$CR""Usage: $ $MY_NAME <NODE_VERSION_TO_INSALL>"
echo "Example: $ $MY_NAME v0.12.1"
echo "Example: $ $MY_NAME $CR"
exit 1
fi
echo """$CR""First, run: $ brew update"
echo "Likely, you'll need to do what it suggests."
echo "Likely, you'll need to run: $ brew update$CR"
echo "To install latest node version, run the following command to get the latest version: $ nvm ls-remote"
echo "... and pass the version number you want as the only param to $MY_NAME. $CR"
echo "Are you ready to install the latest version of nvm and npm and node version $NODE_VER_TO_INSTALL ?$CR"
echo "Press CTL+C to exit --or-- Enter to continue..."
read x
echo """$REV""Uninstalling nvm...$CR$OFF"
# Making backups, but in all likelyhood you'll just reinstall them (and won't need these backups)
if [ ! -d "$BACKUP_DIR" ]; then
echo "Creating directory to store $HOME/.nvm .npm and .bower cache backups: $BACKUP_DIR"
mkdir -p $BACKUP_DIR
fi
set -x
mv $HOME/.nvm $BACKUP_DIR 2>/dev/null
mv $HOME/.npm $BACKUP_DIR 2>/dev/null
mv $HOME/.bower $BACKUP_DIR 2>/dev/null
{ set +x; } &>/dev/null
echo "$REV""$CR""Uninstalling node...$CR$OFF"
echo "Enter your password to remove user some node-related /usr/local directories"
set -x
sudo rm -rf /usr/local/lib/node_modules
rm -rf /usr/local/lib/node
rm -rf /usr/local/include/node
rm -rf /usr/local/include/node_modules
rm /usr/local/bin/npm
rm /usr/local/lib/dtrace/node.d
rm -rf $HOME/.node
rm -rf $HOME/.node-gyp
rm /opt/local/bin/node
rm /opt/local/include/node
rm -rf /opt/local/lib/node_modules
rm -rf /usr/local/Cellar/nvm
brew uninstall node 2>/dev/null
{ set +x; } &>/dev/null
echo "$REV""$CR""Installing nvm...$CR$OFF"
echo "++brew install nvm"
brew install nvm
echo '$(brew --prefix nvm)/nvm.sh'
source $(brew --prefix nvm)/nvm.sh
echo "$REV""$CR""Insert the following line in your startup script (ex: $HOME/.bashrc):$CR$OFF"
echo "export NVM_DIR=\"\$(brew --prefix nvm)\"; [ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\"$CR"
NVM_DIR="$(brew --prefix nvm)"
echo """$CR""Using nvm install node...$CR"
echo "++ nvm install $NODE_VER_TO_INSTALL"
nvm install $NODE_VER_TO_INSTALL
NODE_BINARY_PATH="`find /usr/local/Cellar/nvm -name node -type d|head -n 1`/$NODE_VER_TO_INSTALL/bin"
echo "$REV""$CR""Insert the following line in your startup script (ex: $HOME/.bashrc) and then restart your shell:$CR$OFF"
echo "export PATH=\$PATH:$NODE_BINARY_PATH:$HOME/.node/bin"
echo """$CR""Upgrading npm...$CR"
echo '++ install -g npm#latest'
npm install -g npm#latest
{ set +x; } &>/dev/null
echo "$REV""$CR""Insert following line in your $HOME/.npmrc file:$OFF"
echo """$CR""prefix=$HOME/.node$CR"
echo "Now, all is likley well if you can run the following without errors: npm install -g grunt-cli$CR"
echo "Other recommended global installs: bower, gulp, yo, node-inspector$CR"
I wrote a short article here that details why this is "the right way".
If you need to install iojs, do so using nvm like this:
nvm install iojs-v1.7.1
To install brew, just see its home page.
See alexpods answer for the rest.
You should install node.js with nvm, because that way you do not have to provide superuser privileges when installing global packages (you can simply execute "npm install -g packagename" without prepending 'sudo').
Brew is fantastic for other things, however. I tend to be biased towards Bower whenever I have the option to install something with Bower.
Here's what I do:
curl https://raw.githubusercontent.com/creationix/nvm/v0.20.0/install.sh | bash
cd / && . ~/.nvm/nvm.sh && nvm install 0.10.35
. ~/.nvm/nvm.sh && nvm alias default 0.10.35
No Homebrew for this one.
nvm soon will support io.js, but not at time of posting: https://github.com/creationix/nvm/issues/590
Then install everything else, per-project, with a package.json and npm install.
I agree with noa -- if you need to have multiple versions of node, io.js then brew is not the appropriate solution.
You can help beta-test io.js support in nvm: https://github.com/creationix/nvm/pull/616
If you just want io.js and are not switching versions, then you can install the binary distribution of io.js from https://iojs.org/dist/v1.0.2/iojs-v1.0.2-darwin-x64.tar.gz ; that includes npm and you will not need nvm if you are not switching versions.
Remember to update npm after installing: sudo npm install -g npm#latest
For install with zsh and Homebrew:
brew install nvm <=== This is not recommended by NVM. They want to run their shell script instead
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Then Add the following to ~/.zshrc or your desired shell
configuration file:
export NVM_DIR="$HOME/.nvm"
. "/usr/local/opt/nvm/nvm.sh"
Then install a node version and use it.
nvm install 7.10.1
nvm use 7.10.1
2021 Update
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
Troubleshooting for MAC:
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.
If you use bash, the previous default shell, run touch ~/.bash_profile to create the necessary profile file if it does not exist.
You might need to restart your terminal instance or run . ~/.nvm/nvm.sh. Restarting your terminal/opening a new tab/window, or running the source command will load the command and the new configuration.
You have previously used bash, but you have zsh installed. You need to manually add these lines to ~/.zshrc and run . ~/.zshrc.

nvm keeps "forgetting" node in new terminal session

Upon using a new terminal session in OS X, nvm forgets the node version and defaults to nothing:
$ 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.
My .profile for the brew version:
#nvm
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
And for the install.sh script:
$ curl https://raw.githubusercontent.com/creationix/nvm/v0.10.0/install.sh | bash
#nvm
export NVM_DIR="/Users/farhad/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
Any clue to what I'm doing wrong?
Try nvm alias default. For example:
$ nvm alias default 0.12.7
This sets the default node version in your shell. Then verify that the change persists by closing the shell window, opening a new one, then:
node --version
Alias to node itself to avoid updating the default alias along with node version updates later on.
nvm alias default node
In my case, another program had added PATH changes to .bashrc
If the other program changed the PATH after nvm's initialisation, then nvm's PATH changes would be forgotten, and we would get the system node on our PATH (or no node).
The solution was to move the nvm setup to the bottom of .bashrc
### BAD .bashrc ###
# NVM initialisation
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
# Some other program adding to the PATH:
export PATH="$ANT_ROOT:$PATH"
Solution:
### GOOD .bashrc ###
# Some other program adding to the PATH:
export PATH="$ANT_ROOT:$PATH"
# NVM initialisation
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
(This was with bash 4.2.46 on CentOS. It seems to me like a bug in bash, but I may be mistaken.)
To install the latest stable version:
nvm install stable
To set default to the stable version (instead of a specific version):
nvm alias default stable
To list installed versions:
nvm list
As of v6.2.0, it will look something like:
$ nvm list
v4.4.2
-> v6.2.0
default -> stable (-> v6.2.0)
node -> stable (-> v6.2.0) (default)
stable -> 6.2 (-> v6.2.0) (default)
iojs -> N/A (default)
nvm does its job by changing the PATH variable, so you need to make sure you aren't somehow changing your PATH to something else after sourcing the nvm.sh script.
In my case, nvm.sh was being called in .bashrc but then the PATH variable was getting updated in .bash_profile which caused my session to find the system node before the nvm node.
Here is a simple instruction:
1) Install:
nvm install 8.10.0
2) Use once per terminal
nvm use 8.10.0
3) Set up as default for all terminals
nvm alias default 8.10.0
You may need to use root permissions to perform those actions.
And don't forget to check nvm documentation for more info.
Also note that you may need to specify node version for your IDE:
None of these solutions worked in my environment, nvm always seems to load the first installed version of node no matter what (unless you change it temporarily via nvm use).
The only way to change the default I have found is to:
Clear nvm cache: nvm cache clear
Set default to desired version: nvm alias default 12 (or whatever version)
Switch to desired version: nvm use 12
Uninstall all other versions:
nvm ls (to list installations)
nvm uninstall x (run for each installation that is not the default)
Reinstall other versions: nvm install x
You can use this script to automate this process (just change the first variable to your desired version) - it will re-install all versions you had previously automatically.
DEFAULT_NVM_VERSION=16
nvm cache clear
nvm install $DEFAULT_NVM_VERSION
nvm alias default $DEFAULT_NVM_VERSION
NVERS=$(nvm ls --no-alias | grep -v -- "->" | grep -o "v[0-9.]*")
while read ver; do nvm uninstall $ver; done <<< $NVERS
while read ver; do nvm install $ver; done <<< $NVERS
nvm use $DEFAULT_NVM_VERSION
Or as a one-liner:
DEFAULT_NVM_VERSION=16 && nvm cache clear && nvm install $DEFAULT_NVM_VERSION && nvm alias default $DEFAULT_NVM_VERSION && NVERS=$(nvm ls --no-alias | grep -v -- "->" | grep -o "v[0-9.]*") && while read ver; do nvm uninstall $ver; done <<< $NVERS && while read ver; do nvm install $ver; done <<< $NVERS && nvm use $DEFAULT_NVM_VERSION
New terminals should now respect the default version.
The top rated solutions didn't seem to work for me. My solution is below:
Uninstall nvm completely using homebrew:brew uninstall nvm
Reinstall brew install nvm
In Terminal, follow the steps
below(these are also listed when installing nvm via homebrew):
mkdir ~/.nvm
cp $(brew --prefix nvm)/nvm-exec ~/.nvm/
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
The steps outlined above will add NVM's working directory to your $HOME path, copy nvm-exec to NVM's working directory and add to $HOME/.bashrc, $HOME/.zshrc, or your shell's equivalent configuration file.(again taken from whats listed on an NVM install using homebrew)
I'm using ZSH so I had to modify ~/.zshrc with the lines concerning NVM in that order:
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
source ~/.nvm/nvm.sh
If you have tried everything still no luck you can try this :_
1 -> Uninstall NVM
rm -rf ~/.nvm
2 -> Remove npm dependencies by following this
3 -> Install NVM
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
4 -> Set ~/.bash_profile configuration
Run sudo nano ~/.bash_profile
Copy and paste following this
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
5 -> CONTROL + X save the changes
6 -> Run . ~/.bash_profile
7 -> Now you should have nvm installed on your machine, to install node run nvm install v7.8.0 this will be default node version or you can install any version of node
This question has mentioned for the OSX, but it happened to me in my linux OS.
I tried using nvm alias default <version> but for each new terminal session the used node version was forgotten.
so, here is the solution that i figured out.
make sure to set a default alias for node version,put the following code in .bashrc, and source .bashrc.
export NVM_DIR="/home/bonnie/.nvm"
## If the file exists and is not empty
if [ -s "$NVM_DIR/nvm.sh" ]; then
## Source it
source "$NVM_DIR/nvm.sh"
fi
NODE_DEFAULT_VERSION=$(<"$NVM_DIR/alias/default")
export PATH="$NVM_DIR/versions/node/$NODE_DEFAULT_VERSION/bin":$PATH
descriptive solution link
Doing nvm install 10.14, for example, will nvm use that version for the current shell session but it will not always set it as the default for future sessions as you would expect. The node version you get in a new shell session is determined by nvm alias default. Confusingly, nvm install will only set the default alias if it is not already set. To get the expected behaviour, do this:
nvm alias default ''; nvm install 10.14
This will ensure that that version is downloaded, use it for the current session and set it as the default for future sessions.
run this after you installed any version,
n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local
This command is copying whatever version of node you have active via nvm into the /usr/local/ directory and setting the permissions so that all users can access them.
I have found a new way here. Using n Interactively Manage Your Node.js helps.
I was facing the same issue while using the integrated terminal in VS Code editor. Restarting VS Code after changing the node version using nvm fixed the issue for me.
I use NVM with zsh bash
follow this link to remove nvm
$ brew install nvm
I ran this line $ source $(brew --prefix nvm)/nvm.sh after installation and restart terminal
place the line bellow in ~/.zshrc file, also instructed by nvm official GitHub page
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
# place this after nvm initialization!
autoload -U add-zsh-hook
load-nvmrc() {
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$node_version" ]; then
nvm use
fi
elif [ "$node_version" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc
For some reason in my .bashrc file I found this:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" --no-use # This loads nvm
and had to remove --no-use flag, which I don't remember putting there in a first place...
Just another thing to check.
If you also have SDKMAN...
Somehow SDKMAN was conflicting with my NVM. If you're at your wits end with this and still can't figure it out, I just fixed it by ignoring the "THIS MUST BE AT THE END OF THE FILE..." from SDKMAN and putting the NVM lines after it.
#THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!
export SDKMAN_DIR="/Users/myname/.sdkman"
[[ -s "/Users/myname/.sdkman/bin/sdkman-init.sh" ]] && source "/Users/myname/.sdkman/bin/sdkman-init.sh"
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
On my end, I had to change both aliases STABLE and DEFAULT
nvm alias stable {node_version}
nvm alias default {node_version}
$ nvm alias default {NODE_VERSION}
when we use the above command, only update the node version but the npm still uses the old version.
Here is another solution for update the both node and npm, in my case i want to use node 8.9.4 and i have used the below command.
$ nvm use default 8.9.4
And the command returns the output.
Now using node v8.9.4 (npm v5.6.0)
As mentioned in the repository's issues section, nvm use is just for a lifetime of the shell. I have found this very useful, but sometimes it may put you in trouble actually when you are working on different codebases which need different versions of code.
This is the link for the related discussion in GitHub
For some reason, in my ~/.profile file I found that was selecting the version of node, overriding the alias default command of nvm
Just another thing to check.
Linux/ubuntu
how to solve this you can see here
https://i.ibb.co/vQpMrpb/2022-11-19-18-14.jpg
nvm use isn't meant to persist - it's only for the lifetime of the shell.
You can either do nvm alias default node if you want that to be the default when opening new shells or, you can make a .nvmrc file that will take precedence anywhere in the current directory, upwards to /.
https://github.com/nvm-sh/nvm/issues/658
Also in case you had node installed before nvm check in your ~/.bash_profile to not have something like :
export PATH=/bin:/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:$PATH
If you do have it, comment/remove it and nvm should start handling the default node version.
On Ubuntu there is a potential issue if you are running a non-interactive shell, for example from a cronjob, or an init or upstart script.
Ubuntu's default .bashrc has this guard at the top which drops out early if the shell is non-interactive:
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
So in order to get NVM loaded for non-interactive scripts, you can edit ~/.bashrc and move NVM's init to the top of the file, just above the guard.
Even safer is to copy the NVM init so it appears twice. That will address the concern mentioned in other answers, when other lines are modifying the PATH after NVM. NVM doesn't seem to mind being run twice, although it does slow down the shell startup a little.
In the nvm autoload script from the github I had to chage
local node_version="$(nvm version)" to local node_version="$(node -v)"
There was a local install of nvm on my system in my path so nvm version kept saying system no matter what
1.- Install via homebrew
2.- Because I am using zsh terminal, in ~/.zshrc add this lines, if you are using bash you will need to put that lines in ~/.bash_profile
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
Ref: The issue that is causing this is related to the fact that the folder NPM is trying to access/write to is not owned by the user that is executing the command
The run the commands stated below and You should be good to go!
sudo chown -R `whoami` ~/.npm
sudo chown -R `whoami` /usr/local/lib/node_modules
Try running npm install -g ... or npm uninstall -g ... without sudo and it should run without errors.

Installed node.js ver 0.8 but node --version still shows previous version 0.6.12

I tried installing node ver 0.8 on my ubuntu 12.04.It already has a node ver 0.6.12.The installation went suceesfully but when i type in
node --version
it still shows previous version.
i tried to remove previous version using sudo apt-get remove node but it says package node is not installed.But on trying node --version it shows 0.6.12
Why is it so??
The problem is, you need to replace the new location for node with the old in your PATH variable. If you have an old manual install, find the old path to node by running echo $PATH. Then run this command:
export PATH=${PATH%$OLD_NODE_PATH/bin*}$NEW_NODE_PATH/bin${PATH#$*OLD_NODE_PATH/bin}
Or if you are using an install from the apt-get repository, just run:
export PATH=$NEW_NODE_PATH/bin
And that should fix your problem. But there is a better way! The best tool to manage your node.js environment is NVM. It exactly like RVM for ruby and similar to virtualenv for python, if you are familiar with those tools. It allows you to switch versions of node and download new ones extremely efficiently, and is easy to use. Download and install with:
curl https://raw.github.com/creationix/nvm/master/install.sh | sh
Then add this line to your bash (assuming you are running a bash shell) where it will be loaded (I prefer .bash_login for the personal stuff although it is not loaded by default):
[[ -s $HOME/.nvm/nvm.sh ]] && . $HOME/.nvm/nvm.sh
Source your bash script or restart the terminal then enter this command:
nvm install 0.8.0 && nvm use 0.8.0
This should set you up just fine. Although not necessary, you should probably get rid of all the other node installs, for the sake of tidiness. Check out their github page but to get you started here is a quick overview:
nvm ls # list all installed versions of node
nvm ls-remote # list all available versions of node
nvm install 0.9.8 # download and install node v0.9.8
nvm use 0.8.0 # switch current environment to use node v0.8.0
nvm alias default 0.8.0 # set 0.8.0 as default, you can use 'nvm use default'
nvm deactivate # use system install of node
nvm run default app.js # run app.js with default node version
I had this issue until I followd the directions on
https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager
which included running:
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
first. Then running sudo apt-get install nodejs npm got me to 0.8.x
Also see: http://apptob.org/
Seem like you install nodejs package from Ubuntu repo and manually install node 0.8 after?
Try remove nodejs package.
The way to get a more recent version of Node.js is to add a PPA (personal package archive) maintained by NodeSource. This will probably have more up-to-date versions of Node.js than the official Ubuntu repositories.
First, you need to install the PPA in order to get access to its contents:
curl -sL https://deb.nodesource.com/setup | sudo bash -
The PPA will be added to your configuration and your local package cache will be updated automatically. After running the setup script from nodesource, you can install the Node.js package using the below command.
sudo apt-get install nodejs
You can check the node by using this command
node -v

Resources