How to use the command Set-NodeVersion (with examples)
Set-NodeVersion is a command in the ps-nvm PowerShell module that allows you to set the default version of Node.js in your PowerShell session. It is particularly useful when you want to switch between different versions of Node.js or ensure that a specific version is used consistently.
Use case 1: Use a specific version of Node.js in the current PowerShell session
Set-NodeVersion node_version
Motivation: You may have multiple versions of Node.js installed on your machine and want to use a specific version for a particular project or task. By using the Set-NodeVersion command, you can easily switch between different versions without having to modify system-wide settings.
Explanation:
node_version
: The version number of the desired Node.js version you want to use.
Example output:
Default Node.js version for current session set to node_version.
Use case 2: Use the latest installed Node.js version 20.x
Set-NodeVersion ^20
Motivation: If you have multiple versions of Node.js installed and want to ensure that you are using the latest version within a specific major version, this command comes in handy. This allows you to leverage the latest features and bug fixes without manually selecting the version every time.
Explanation:
^20
: A version range syntax where ‘^’ is used to indicate that any version in the 20.x range can be used.
Example output:
Default Node.js version for current session set to the latest installed version in the 20.x range.
Use case 3: Set the default Node.js version for the current user
Set-NodeVersion node_version -Persist User
Motivation: You may want to set a default Node.js version for all future PowerShell sessions of the current user. This can be useful when you have a preferred version that you want to use consistently across different projects.
Explanation:
node_version
: The version number of the desired Node.js version you want to set as the default.-Persist User
: This flag specifies that the default Node.js version should persist for all future PowerShell sessions of the current user.
Example output:
Default Node.js version set to node_version for all future PowerShell sessions of the current user.
Use case 4: Set the default Node.js version for all users
Set-NodeVersion node_version -Persist Machine
Motivation: As an administrator or root user, you may need to set a default Node.js version that applies to all users on the machine. This ensures consistency across different user accounts and avoids the need for each individual user to set their preferred version.
Explanation:
node_version
: The version number of the desired Node.js version you want to set as the default.-Persist Machine
: This flag specifies that the default Node.js version should persist for all future PowerShell sessions of all users on the machine. This command must be run with administrator or root privileges.
Example output:
Default Node.js version set to node_version for all future PowerShell sessions of all users.
Conclusion:
The Set-NodeVersion command is a powerful tool when working with Node.js in PowerShell. It allows you to easily switch between different versions of Node.js, set defaults for individual users or all users, and leverage the latest installed versions. Whether you are a developer working on multiple projects or an administrator managing Node.js environments, this command can streamline your workflow and ensure consistent version control.