How to use the command 'nodenv' (with examples)
Nodenv is a tool used for managing different versions of Node.js. It allows you to easily switch between Node.js versions based on your project requirements. This article will guide you through various use cases of the ’nodenv’ command.
Use case 1: Install a specific version of Node.js
Code:
nodenv install version
Motivation: If you need to work with a specific version of Node.js for your project, you can use this command to install it.
Explanation: Replace ‘version’ with the desired Node.js version number you wish to install. For example, ‘10.19.0’ or ‘14.17.0’.
Example output:
Downloading node-v10.19.0.tar.gz...
-> https://nodejs.org/dist/v10.19.0/node-v10.19.0.tar.gz
Installing node-v10.19.0...
Installed node-v10.19.0 to /Users/user/.nodenv/versions/10.19.0
Use case 2: Display a list of available Node.js versions
Code:
nodenv install --list
Motivation: This command allows you to see a list of the available Node.js versions that you can install.
Explanation: By calling the ‘install’ subcommand with the ‘–list’ option, nodenv will list all the available Node.js versions that you can install.
Example output:
Available versions:
10.19.0
12.22.1
14.17.0
Use case 3: Use a specific version of Node.js across the whole system
Code:
nodenv global version
Motivation: If you want to use a specific version of Node.js globally across your system, this command sets the chosen version for all users and directories.
Explanation: Replace ‘version’ with the desired Node.js version number you want to set as the global version.
Example output:
Set global version to 12.22.1
Use case 4: Use a specific version of Node.js with a directory
Code:
nodenv local version
Motivation: If you have multiple projects that require different Node.js versions, you can use this command to set a specific version for a particular directory.
Explanation: Replace ‘version’ with the desired Node.js version number you want to set for the directory where you execute the command.
Example output:
Set local version to 14.17.0
Use case 5: Display the Node.js version for the current directory
Code:
nodenv version
Motivation: This command is useful when you want to check which Node.js version is currently set for the directory you are in.
Explanation: This command displays the Node.js version that is set for the current directory.
Example output:
12.22.1
Use case 6: Display the location of a Node.js installed command
Code:
nodenv which command
Motivation: If you want to know the location of a specific Node.js command, such as ’npm’, this command helps you find it.
Explanation: Replace ‘command’ with the name of the Node.js command you want to know the location of, such as ’npm’.
Example output:
/Users/user/.nodenv/versions/12.22.1/bin/npm
Conclusion:
The ’nodenv’ command is a powerful tool for managing Node.js versions. It allows you to easily switch between different versions based on your project’s requirements. With the use cases discussed in this article, you can effectively install, switch between, and manage Node.js versions across your system and directories.