Installing Node.js via Package Manager
Installating Node.js via Package Manager
Ref: Node.js - Installing Node.js via package manager
$ sudo apt install nodejs npm
Update npm to latest version:
$ sudo npm install -g npm
Verifing Node.js and npm (Node Package Manager):
$ node -v
$ npm -v
Installing Node.js using Setup Script
Ref: https://github.com/nodesource/distributions/blob/master/README.md#debinstall
For Ubuntu 18.04 and higher:
$ curl -fsSL https://deb.nodesource.com/setup_<version>.x | sudo -E bash -
which
$ sudo apt install -y nodejs
Install build tools (Optional):
$ sudo apt install -y build-essential
Verifing NodeJS:
$ which node
$ node -v
Updating NodeJS with with NPM (Node Package Manager)
First, clear the npm cache:
$ npm cache clean -f
Install n, Node’s version manager:
$ npm install -g n
With the n module installed, you can use it to:
Install the latest stable version:
$ sudo n stable
Install the latest release:
$ sudo n latest
Install a specific version:
$ sudo n [version.number]
Update Node.js with Binary Packages
- Navigate to Node’s official downloads page where you can find all available packages. There you can download the source code or pre-built installer for the LTS versions or the latest release.
- You can either download the package from your browser or find the version number you need and add it to the wget command:
wget https://nodejs.org/dist/v14.17.0/node-v14.17.0-linux-x64.tar.xz
- Next, install xz-utils used to extract the binary package:
$ sudo apt-get install xz-utils 4. Extract and install the package with the command:
$ sudo tar -C /usr/local --strip-components 1 -xJf node-v14.17.0-linux-x64.tar.xz
Installing NodeJS via NVM (Node Version Manager)
Install NVM
Check latest version:
https://github.com/nvm-sh/nvm/releases/latest
Install using script:
$ NVM_VERSION=v0.39.7
$ wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/${NVM_VERSION}/install.sh | bash
Source the profile:
$ source ~/.bashrc
Verify Installation:
$ command -v nvm
$ nvm --version
(Optional) Deeper Shell Integration
Create .nvmrc in project directory:
To make nvm default to the latest 5.9 release, the latest LTS version, or the latest node version for the current directory:
$ cd /path/to/project
$ echo "5.9" > .nvmrc
$ echo "lts/*" > .nvmrc # to default to the latest LTS version
$ echo "node" > .nvmrc # to default to the latest version
Then when you run nvm:
$ nvm use
Found '/path/to/project/.nvmrc' with version <5.9>
Now using node v5.9.1 (npm v3.7.3)
To automatically call nvm use in a directory with a .nvmrc file, put the following at the end of your $HOME/.bashrc:
$ nano ~/.bashrc
cdnvm() {
command cd "$@" || return $?
nvm_path="$(nvm_find_up .nvmrc | command tr -d '
')"
# If there are no .nvmrc file, use the default nvm version
if [[ ! $nvm_path = *[^[:space:]]* ]]; then
declare default_version
default_version="$(nvm version default)"
# If there is no default version, set it to `node`
# This will use the latest version on your machine
if [ $default_version = 'N/A' ]; then
nvm alias default node
default_version=$(nvm version default)
fi
# If the current version is not the default version, set it to use the default version
if [ "$(nvm current)" != "${default_version}" ]; then
nvm use default
fi
elif [[ -s "${nvm_path}/.nvmrc" && -r "${nvm_path}/.nvmrc" ]]; then
declare nvm_version
nvm_version=$(<"${nvm_path}"/.nvmrc)
declare locally_resolved_nvm_version
# `nvm ls` will check all locally-available versions
# If there are multiple matching versions, take the latest one
# Remove the `->` and `*` characters and spaces
# `locally_resolved_nvm_version` will be `N/A` if no local versions are found
locally_resolved_nvm_version=$(nvm ls --no-colors "${nvm_version}" | command tail -1 | command tr -d '\->*' | command tr -d '[:space:]')
# If it is not already installed, install it
# `nvm install` will implicitly use the newly-installed version
if [ "${locally_resolved_nvm_version}" = 'N/A' ]; then
nvm install "${nvm_version}";
elif [ "$(nvm current)" != "${locally_resolved_nvm_version}" ]; then
nvm use "${nvm_version}";
fi
fi
}
alias cd='cdnvm'
cdnvm "$PWD" || exit
Source bash profile changes:
$ source ~/.bashrc
(Optional) Bash Completion
Put the below sourcing line just below the sourcing line for nvm in profile (.bashrc or .bash_profile).
$ nano ~/.bashrc
#! ~/.bashrc
[[ -r $NVM_DIR/bash_completion ]] && \. $NVM_DIR/bash_completion
$ source ~/.bashrc
Usage
nvm:
$ nvmTab
Install NodeJS
<node_version> can be one of (17, 16, 14, 12).
To install latest version of Node:
$ nvm install node
To install specific version of Node:
$ nvm install <node_version>;
To see all versions of Node that are installed on local machine:
$ nvm ls
To switch to specific version:
$ nvm use <node_version>
To set default specific version:
$ nvm alias default <node_version>
Update NodeJS
Before upgrading Node.js, check which version is running on the system:
$ nvm ls
Check for newly available releases with:
$ nvm ls-remote
To update the latest version, use the nvm command with the specific Node.js version:
$ nvm install [version.number]
E.g: $ nvm install 14 (or) $ nvm install 14.x.x
Uninstall NodeJS
To uninstall latest version of Node use command:
$ nvm uninstall node
To uninstall specific version of Node (for example - 14):
$ nvm uninstall 14
Packages location:
~/.nvm/versions/node/
UnInstall NPM
Remove any nvm lines from ~/.bash_profile , ~/.bashrc(and/or ~/.profile) and re-source the bash profile:
$ nano ~/.bashrc
...
$ source ~/.bashrc
Remove all directories previously created:
$ rm -rf ~/.nvm
$ rm -rf ~/.npm
$ rm -rf ~/.bower
NodeJS Usage
Install/Update Packages
Use with -g if install/update package globally.
Update npm (NodeJS Package Manager):
$ npm i -g npm
Install/Update yarn:
$ npm install -g yarn
Install/Update node-sass:
$ npm i -g node-sass
Run and Fix Audit
$ npm audit
$ npm i --package-lock-only
$ npm audit fix
Errors and Issues
Issue: Node Sass could not find a binding for your current environment: Linux 64-bit with Node.js 12.x.
$ npm rebuild node-sass --force</version></node_version></node_version></node_version></node_version></version></version>