unable to switch version of node from 8 to 10 on mac - node.js

I tried various options available over the internet as listed below, but still node v8 is there even though I restarted my system as well-
sudo npm install n -g
sudo n stable
npm install -g n
sudo npm cache clean -f sudo npm install -g n sudo n stable
brew update brew upgrade brew upgrade node
brew update && brew upgrade node && npm update -g npm
sudo npm cache clean -f sudo npm install -g n
sudo rm -rf /opt/local/bin/node /opt/local/include/node
/opt/local/lib/node_modules sudo rm -rf /usr/local/bin/npm
/usr/local/share/man/man1/node.1 /usr/local/lib/dtrace/node.d
brew uninstall --force node brew unlink node brew install node#10

use NVM to switch node versions, you can use the install script directly, restart the shell and start using it like:
nvm install <node_version>
if you want to get the list of available versions you can:
nvm ls-remote
if you want to see what versions has already been installed:
nvm ls
to switch a version is:
nvm use <node_version> # the version should be already installed using nvm install
and to set up a default version you can alias it like this:
nvm alias default
Hope it helps.

Mac OS with nvm installed. I was trying to change node from 10 to 12.
In my case, nvm use *.* didn't work.
It used to work once with nvm use 10, but later when I needed to switch to Node 12, nvm use 12 didn't work for me like other attempts to change.
So the steps that helped:
which -a node showed me that I have multiple paths to the node:
``
/usr/local/opt/node#10/bin
/Users/username/.nvm/versions/node/v12.22.7/bin/node
/opt/local/bin/node
/usr/local/bin/node
I have commented out (removed) the line with the unwanted path /usr/local/opt/node#10/bin in the files:
~/.bash_profile
~/.zshrc
Step 2 unfrozen the nvm use [version] command on my Mac.
Maybe it help someone.

Related

Update Node JS on Ubuntu

Ubuntu 20.04.3 LTS
Version 14.0.0 is installed, node -v always shows it. Although in the list of programs Node appears as 16.13.1. I can manually change the version via nvm, but this is very inconvenient. How to make sure that there is no ghost on my computer about version 14?
You can use n module from npm in order to upgrade node
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
to update to the latest version use
sudo n latest
Explained
I solved this problem
rm -rf ~/.nvm
nvm install 16

npm update broke npm

I just followed this guide to update npm (as my nodered camera module wasn't working) and ran
npm install -g npm
but now my npm install seems completely broken. If I just type
npm
or
npm update
I get
/usr/local/lib/node_modules/npm/bin/npm-cli.js:79
let notifier = require('update-notifier')({pkg})
^^^
SyntaxError: Block-scoped declarations (let, const, function, class)
not yet supported outside strict mode
I've tried
sudo apt-get remove npm
sudo apt-get install npm
but the reinstall didn't help.
I think my node version needs upgrading from v4.8.2 but I thought that was only possible with npm?
You probably have npm installed twice, one is in /usr/local/bin and the other in /usr/bin.
First, you can try to remove the npm module that has been installed by upgrading npm. Try to run this:
rm -r /usr/local/lib/node_modules/npm
/usr/bin/npm uninstall npm
Once you have a running version of npm, install a more recent version of node before upgrading npm. Then, remove the version of your linux distribution.
If the first solution doesn't work, another approach is to install a recent version of node (without using npm of course):
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
(solution for centos....I assume it would work also on ubuntu):
to clean up completely my centos machine, I have additionally done the following - my user is "centos" and my home is /home/centos:
sudo rm -rf /usr/local/bin/npm
sudo rm -rf /usr/local/bin/npx
sudo rm -rf /usr/lib/node_modules/
sudo rm -rf /usr/bin/npm
sudo rm -r /usr/local/lib/node_modules/
sudo rm -rf /usr/local/bin/node
sudo rm -rf /usr/bin/npm
sudo rm -rf /usr/lib/node_modules/
rm -rf /home/centos/.npm/
rm -rf /home/centos/node*
rm -rf /home/centos/.node-gyp/
sudo rm -rf /root/.npm/
sudo rm /usr/bin/node
sudo rm -rf /usr/local/include/node
only at this point I reinstalled again:
wget http://nodejs.org/dist/latest/node-v11.4.0-linux-x64.tar.gz
sudo tar --strip-components 1 -xzvf node-v* -C /usr/local
and things are working again:
node --version
v11.4.0
npm --version
6.4.1
To those who used google to find this, you may be tempted to install via
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash - and then installing with sudo apt install nodejs.
However, I somehow ran into this issue regardless. Please keep in mind that npm#6 dropped support for node#<=4, and that is a contributing factor here. If you want to be sure that everything is installed at the latest, correct versions, I very highly recommend installing through nvm.
Via the nvm instructions on their GitHub: You can add the install script with
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
Then you can start using nvm. You will likely have to restart your terminal, so after installation, exit the terminal, start it up again, and check that nvm is installed with nvm --version.
If everything goes well, you can install any specific version of node with npm in tow. The latest stable version of node as of writing this is 10.15.3, so
nvm install 10.15.3
And of course, if you need help, nvm --help has a list of options.
If you are using nvm to install npm and node, try this solution.
Get to know where exactly is the currently used node and npm is installed:
which node
In my case, it was /home/ubuntu/.nvm/versions/node/
Now, delete all the versions of node using:
sudo rm -rf /home/ubuntu/.nvm/versions/node/
You can now use nvm to install your required version of node and npm.
nvm install 4.9.1
Other answers didn't work for me on Ubuntu and ended up in a dead end, with a broken npm or unable to reinstall/update npm.
The radical solution I used :
1/ Remove all traces of node. Follow this page, using the remove.sh script at the bottom :
http://kselax.ru/en/npm-errors/
2/ Then reinstall from scratch nodejs + npm using the latest install script :
https://github.com/nodesource/distributions/blob/master/README.md
For me, reinstalling npm worked:
npm install -g npm

How do I find the old version of node js?

I'm trying to install "n", the nom helper on Amazon Linux. I'm having difficulty. It seems I have an old version of the node somewhere but I can't figure out where. When I run
npm install -g n
I get the below error ...
[myuser#mymachine ~]$ sudo /usr/local/bin/npm install -g n
ERROR: npm is known not to run on Node.js v0.10.48
You'll need to upgrade to a newer version in order to use this
version of npm. Supported versions are 4, 6, 7, 8. You can find the
latest version at https://nodejs.org/
[myuser#mymachine ~]$ npm -v
5.4.2
╭─────────────────────────────────────╮
│ │
│ Update available 5.4.2 → 5.5.1 │
│ Run npm i -g npm to update │
│ │
╰─────────────────────────────────────╯
But notice that when I run
[myuser#mymachine ~]$ node -v
v8.8.1
it tells me I have v8.8.1 installed, which is what I intended. How do I purge the old, unwanted version of the node so I can install my helper?
Easiest solution would be to try the following to cleanup your node issues and reinstall a clean version.
First remove everything related to node
sudo apt-get purge --auto-remove nodejs npm
UPDATE For yum:
yum clean all
yum -y remove nodejs
Remove these leftover files and folders as well
sudo rm -rf /usr/local/bin/npm /usr/local/share/man/man1/node* /usr/local/lib/dtrace/node.d ~/.npm ~/.node-gyp /opt/local/bin/node opt/local/include/node /opt/local/lib/node_modules
Then install node back with nvm,
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.6/install.sh | bash
//To uninstall a node version
//nvm uninstall <current version>
nvm install 8.8.1
nvm use 8.8.1
//check with
node -v
npm -v
//**UPDATE**: Install your package
npm install -g n
And all should work.
UPDATE : Install Without NVM
yum install -y gcc-c++ make
curl -sL https://rpm.nodesource.com/setup_8.x | sudo -E bash -
yum install nodejs
node -v
//Install your package
npm install -g n
check the releases notes of node https://nodejs.org/en/download/releases/ you can download older version from this site
An alternative to installing Node.js through apt is to use a specially designed tool called nvm, which stands for "Node.js version manager".
Using nvm, you can install multiple, self-contained versions of Node.js which will allow you to control your environment easier. It will give you on-demand access to the newest versions of Node.js, but will also allow you to target previous releases that your app may depend on.
To start off, we'll need to get the software packages from our Ubuntu repositories that will allow us to build source packages. The nvm script will leverage these tools to build the necessary components:
sudo apt-get update
sudo apt-get install build-essential libssl-dev
Once the prerequisite packages are installed, you can pull down the nvm installation script from the project's GitHub page. The version number may be different, but in general, you can download it with curl:
curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh -o install_nvm.sh
And inspect the installation script with nano:
nano install_nvm.sh
Run the script with bash:
bash install_nvm.sh
It will install the software into a subdirectory of your home directory at ~/.nvm. It will also add the necessary lines to your ~/.profile file to use the file.
To gain access to the nvm functionality, you'll need to log out and log back in again, or you can source the ~/.profile file so that your current session knows about the changes:
source ~/.profile
Now that you have nvm installed, you can install isolated Node.js versions.
To find out the versions of Node.js that are available for installation, you can type:
nvm ls-remote
Output
...
v5.8.0
v5.9.0
v5.9.1
v5.10.0
v5.10.1
v5.11.0
v6.0.0
As you can see, the newest version at the time of this writing is v6.0.0. You can install that by typing:
nvm install 6.0.0
Usually, nvm will switch to use the most recently installed version. You can explicitly tell nvm to use the version we just downloaded by typing:
nvm use 6.0.0
When you install Node.js using nvm, the executable is called node. You can see the version currently being used by the shell by typing:
node -v
Output
v6.0.0
If you have multiple Node.js versions, you can see what is installed by typing:
nvm ls
If you wish to default one of the versions, you can type:
nvm alias default 6.0.0
This version will be automatically selected when a new session spawns. You can also reference it by the alias like this:
nvm use default
Each version of Node.js will keep track of its own packages and has npm available to manage these.

The best way to install node.js on a Mac?

Homebrew and the Node download page install node.js in different locations that apparently conflict. Brew will complain after a 'brew doctor' if both locations are used.
So, is there a preferred way to put node on my Mac(Yosemite 10.10.5).
This is what I'm getting now while attempting to install node via brew.
node-4.1.0 already installed, it's just not linked
DONs-iMac:Erlang-Elixir donfox1$ brew link node
Linking /usr/local/Cellar/node/4.1.0...
Error: Could not symlink share/systemtap/tapset/node.stp
/usr/local/share/systemtap/tapset is not writable.
Have you tried chmoding 755 /usr/local/share/systemtap/tapset so it is writable? That could fix it?
chmod 755 /usr/local/share/systemtap/tapset
brew link node
Also try this if it does not work (may work):
brew cleanup
brew link node
brew uninstall node
brew install node
sudo chown -R $USER /usr/local/share/
brew doctor
brew install node
Uninstall node:
sudo rm -rf /usr/local/share/systemtap/ && brew uninstall node
and install nvm with brew - so you can control you node versions.
Read this article: suggested way to install npm with brew
If you are not sure about what version of node you need now and potentially need in the future I would also reccomend to install NVM - node version manager.
Download the nvm install script via cURL:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
Ensure that nvm was installed correctly with nvm --version, which should return the version of nvm installed.
Install the version of Node.js you want
Install the latest version with nvm install node
Use the latest version with nvm use node
Install the latest LTS version with nvm install --lts
Use the latest LTS verison with nvm use --lts
You can also check the tutorials:
installing-node-js-tutorial-using-nvm-on-mac-os-x-and-ubuntu
Best way to install and use nvm on Mac

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.

Resources