NVM Basic Notes
Node Version Manager (NVM) is a tool that allows you to easily manage multiple versions of Node.js on a single machine. This guide will walk you through how to use NVM effectively.
Table of Contents
- Installation
- Basic NVM Commands
- Additional NVM Commands
- Managing Node Versions
- Working with Packages
- Aliases and Version Management
- Additional NVM Features
- Examples of Common Commands
- Notes
Installation
To install NVM, follow the instructions in the official NVM repository.
Basic NVM Commands
Check Node Version
node -v || node --version
Displays the current version of Node.js installed.
List Locally Installed Versions of Node
nvm ls
Shows all the versions of Node.js installed on your machine.
List Remote Available Versions of Node
nvm ls-remote
Displays all the Node.js versions available for installation.
Install a Specific Version of Node
nvm install <version>
Installs the specified Node.js version. For example:
nvm install 18.16.1
Install Latest LTS (Long Term Support) Version of Node
nvm install --lts
Installs the latest LTS version of Node.js.
Install Latest Stable Version of Node
nvm install stable
Installs the latest stable version of Node.js.
Switch Node Version
nvm use <version>
Switches to the specified version of Node.js. For example:
nvm use 20.5.1
Set Default Version of Node
nvm alias default <version>
Sets a default version of Node.js to be used in new terminals. For example:
nvm alias default 18.16.1
Additional NVM Commands
Run a Node.js Script with a Specific Version
nvm run <version> <script.js>
Runs a Node.js script with the specified version of Node.js. For example:
nvm run 6.10.3 app.js
Execute a Command with a Specific Node Version
nvm exec <version> <command>
Executes a command using the specified version of Node.js. For example:
nvm exec 4.8.3 node app.js
Set Alias for a Node Version
nvm alias <alias_name> <version>
Creates an alias for a Node.js version. For example:
nvm alias default 8.1.0
Use the Latest Version of Node
nvm use node
Switches to the latest version of Node.js.
Use the Latest LTS Version of Node
nvm use --lts
Switches to the latest LTS version of Node.js.
Managing Node Versions
Uninstall a Specific Node Version
nvm uninstall <version>
Uninstalls the specified version of Node.js. For example:
nvm uninstall 6.9.2
Uninstall the Latest LTS Version
nvm uninstall --lts
Removes the latest LTS version of Node.js.
Uninstall NVM
To remove or uninstall NVM, delete the $NVM_DIR folder (usually located at ~/.nvm).
Working with Packages
Install Global Packages for a Specific Node Version
nvm exec <version> npm install -g <package>
Installs a package globally for a specific Node.js version.
List Global Packages for the Current Node Version
nvm exec current npm list -g --depth=0
Lists all global packages for the currently active Node.js version.
Install Local Packages for a Specific Node Version
nvm use <version>
npm install <package>
Installs a package locally for the specific Node.js version.
Aliases and Version Management
Set Default Node Version
nvm alias default <version>
Sets the default Node.js version for new terminals.
Delete an Alias
nvm unalias <alias_name>
Removes the specified alias.
Check Node Executable Path
nvm which <version>
Displays the path to the Node.js executable for the specified version.
Additional NVM Features
Reinstall Global Packages
nvm reinstall-packages <version>
Reinstalls global packages from the current version to another version.
Migrate Global Packages
nvm migrate <version>
Migrates global packages from one version of Node.js to another.
Deactivate the Current Node Version
nvm deactivate
Deactivates the currently active Node.js version.
Update NVM
nvm upgrade
Updates NVM to the latest version.
Examples of Common Commands
Install a Specific Version of Node.js
nvm install 8.0.0
Use a Specific Version of Node.js
nvm use 8.0
Run a Script with a Specific Version of Node.js
nvm run 6.10.3 app.js
Execute a Command with a Specific Version of Node.js
nvm exec 4.8.3 node app.js
Set an Alias for a Node Version
nvm alias default 8.1.0
Use the Latest LTS Version of Node.js
nvm use --lts
Notes:
- To completely uninstall NVM, delete the
$NVM_DIRfolder (typically located in~/.nvm). - Always check the Node.js version you're using by running:
node -v
Compiled all the essential commands in one place to make NVM usage simple and efficient. Happy coding!