How to use the command 'nvm' (with examples)
Node Version Manager (nvm) is a powerful tool used by developers to install, uninstall, and switch between different versions of Node.js seamlessly. Node.js is a widely-used platform for building server-side applications, and having the ability to manage multiple versions easily is essential, especially when working on diverse projects that may require different versions of Node.js. Nvm supports specifying versions using numbers, labels like “stable”, and even system installation defaults. This flexibility makes it a crucial tool for Node.js developers.
Install a specific version of Node.js
Code:
nvm install 14.17.0
Motivation:
Installing a specific version of Node.js can be critical when your project depends on that particular version for consistency or compatibility reasons. For instance, certain libraries or features may only be available or stable in specific Node.js versions.
Explanation:
install
: This is a sub-command of nvm that triggers the installation process.14.17.0
: The version of Node.js you wish to install. By providing this version number, you instruct nvm to fetch and install this particular version.
Example output:
Downloading and installing node v14.17.0...
...
Now using node v14.17.0
Use a specific version of Node.js in the current shell
Code:
nvm use 14.17.0
Motivation:
Switching to a specific Node.js version ensures that the current shell session uses the intended version of Node.js. This is useful when you need to test your code or run applications that are version-sensitive.
Explanation:
use
: This nvm command activates the specified version of Node.js in the current terminal session.14.17.0
: The version of Node.js to use. This must be a version already installed through nvm.
Example output:
Now using node v14.17.0
Set the default Node.js version
Code:
nvm alias default 14.17.0
Motivation:
By setting a default Node.js version, you ensure that every new terminal session automatically uses a specified version of Node.js, simplifying workflows and ensuring consistent environments across sessions.
Explanation:
alias default
: Defines an alias for the default Node.js version. This alias ensures that nvm in new shells automatically switches to this version unless specified otherwise.14.17.0
: The version number to be set as the default one.
Example output:
default -> 14.17.0 (-> v14.17.0)
List all available Node.js versions and highlight the default one
Code:
nvm list
Motivation:
Listing Node.js versions helps developers view all installed versions and quickly identify which version is currently active and which is set as the default. This visibility is essential for maintaining organized development environments.
Explanation:
list
: This command outputs all Node.js versions installed via nvm, indicating which version is currently being used and which version is the default.
Example output:
-> v14.17.0
v16.13.1
v15.5.1
default -> 14.17.0 (-> v14.17.0)
Uninstall a given Node.js version
Code:
nvm uninstall 15.5.1
Motivation:
Uninstalling a Node.js version frees up system resources and keeps your environment clean, especially if certain versions are no longer needed for your projects. This is part of effective version management.
Explanation:
uninstall
: This command removes a specified version of Node.js from your system.15.5.1
: The version number of Node.js to be uninstalled.
Example output:
Uninstalled node v15.5.1
Launch the REPL of a specific version of Node.js
Code:
nvm run 14.17.0 --version
Motivation:
Launching the Read-Eval-Print Loop (REPL) for a specific Node.js version allows developers to interactively experiment with and debug that version’s features and capabilities, ensuring they behave as expected.
Explanation:
run
: This sub-command executes a specified version of Node.js.14.17.0
: The Node.js version for the REPL session.--version
: This argument displays the Node.js version currently being run.
Example output:
Running node v14.17.0
v14.17.0
Execute a script in a specific version of Node.js
Code:
nvm exec 14.17.0 node app.js
Motivation:
Running a script with a specific Node.js version is crucial for testing or deploying applications where version compatibility is a requirement. This ensures the application executes under the exact conditions it was developed and tested for.
Explanation:
exec
: Triggers the execution of a command using the specified Node.js version.14.17.0
: The version of Node.js to use for executing the script.node app.js
: The command and file you wish to run, in this case, a JavaScript file namedapp.js
.
Example output:
Running node v14.17.0
Application started successfully!
Conclusion:
Mastering the use of nvm empowers developers to manage multiple Node.js environments seamlessly, enabling smooth transitions and compatibility across various development projects. By installing, switching, and uninstalling specific Node.js versions as needed, developers can ensure their workflows remain efficient and adapt to any project requirements.