Akik No

NVS (Node Version Switcher)

NVS is a cross-platform version-switching tool for Node.js, and NVS itself is written in Node.js.

This tool is clearly inspired by other Node.js version manager tools, especially NVM, which borrows many ideas and some command line syntax.

Here are the basic setup instructions. For more details and options on setting up NVS, see the Settings page.

Windows

The MSI installation package for Windows is available from the NVS Releases Page on GitHub. You can also install via Chocolatey:

choco install nvs
Copy the code

Mac, Linux

Specify the installation path, clone the repo, and enter the installation command:

export NVS_HOME="$HOME/.nvs"
git clone https://gitee.com/wsz7777/nvs "$NVS_HOME"
. "$NVS_HOME/nvs.sh" install
Copy the code

The nvs.sh is the shell method to add NVS to the environment variable. After executing this script, you can use NVS directly from the command line. Please add the install command to ~/.bashrc,~/.profile, or ~/.zshrc. So that the NVS functionality is available in your shell.

For KSH, the nvs.sh script needs to be added to ~/.kshrc, or $ENV.

CI support

NVS can be used in a CI environment and can be used in Travis CI. Use the NVS test to download any version of node.js environment.

Based on using

Download the latest version of Node.js:

$ nvs add latest
Copy the code

Download the LTS version of Node.js:

$ nvs add lts
Copy the code

Run NVS use to select the version of Node.js

$NVS use LTS PATH += ~/. NVS /node/6.9.1/x64Copy the code

Add the default node.js version using NVS link:

$ nvs link lts
Copy the code

The Command is introduced

The command describe
nvs help <command> Get detailed help for commands
nvs install Initialize and use NVS
nvs uninstall Remove NVS from profiles and environments
nvs --version Show the NVS version
nvs add [version] Download a version of Node.js
nvs rm <version> Remove a version of Node.js
nvs migrate <fromver> [tover] Migrate the global node_modules
nvs upgrade [fromver] Update node.js of the current environment to the latest version
nvs use [version] Choose to use a version of Node.js
nvs auto [on/off] Use CWD to switch automatically
nvs run <ver> <js> [args...] Use a version of Node.js to execute js applications
nvs exec <ver> <exe> [args...] Use a version of Node.js to execute the executable
nvs which [version] Displays the path to a binary file for a version of Node.js
nvs ls [filter] Displays a list of locally downloaded Versions of Node.js
nvs ls-remote [filter] Lists the downloadable versions of Node.js
nvs lsr [filter] Same as above
nvs link [version] Set up a soft connection to point to a version as the default version to use
nvs unlink [version] Remove the link to the default version
nvs alias [name] [value] Set an alias for a version
nvs remote [name] [value] Set up the repository to download node from

[version] and [filter] are used to describe versions in the following cases

situation example
Full version number 15.14.0, 0.6.11
Incomplete version number 14, 15, 8
The label lts, latest, Argon
Name of the remote installation repository The node, the node / 15.12.0. If you use NVS remote to add remote warehouse named Taobao, you can use Taobao, Taobao /15.13.0
The part after the slash of the remote warehouse name LTS, 4.6.0, 6 / x86, the node / 6.7 / x64

Presumably, you can explore more usages on your own see the documentation for more details on each command

The interactive menu

When called without arguments, thenvsDisplays an interactive menu for switching and downloading node.js versions.NVS USESconsole-menu, console-Menu was originally written for this project and then released separately.

VS Code support

Visual Studio Code can use NVS to select the node.js version to use for startup or debugging. Json (.vscode is in the folder in the project root folder) add “runtimeArgs” to the “runtimeExecutable” property with the NVS version string, And point to NVS. CMD (Windows) or NVS (Mac, Linux). (If NVS is not in VS Code’s PATH environment variable, you may need to specify an absolute PATH, such as “${env:HOME}/.nvs/ NVS “)

Example configuration: launch.json using VS Code Using NVS to launch Node.js version 6.10:

  "configurations": [{"type": "node"."request": "launch"."name": "Launch Program"."program": "${file}"."args": []."runtimeArgs": [ "6.10"]."windows": { "runtimeExecutable": "nvs.cmd" },
      "osx": { "runtimeExecutable": "nvs" },
      "linux": { "runtimeExecutable": "nvs"}},]Copy the code

Alternatively, remove the version string from it and “runtimeArgs” gets the version from the. Node-version file or folder. For more details, see the NVS VS Code documentation or run the command NVS help vscode.

Configuration remotes

The NVS remote command allows you to configure multiple named download locations. NVS manage versions from different remote locations separately, so there is no risk of version conflicts. By default, there is only one remote pointing to the official Node.js version:

$ nvs remote
default  node
node     https://npm.taobao.org/mirrors/node/
Copy the code

This allows you to get builds from other sources. The following command sequence adds a remote remote to the nightly, lists the nightly, and adds a build:

$ nvs remote add nightly https://nodejs.org/download/nightly/ $ nvs lsr nightly/13 The nightly / 13.1.1 - nightly20191120c7c566023f... $ nvs add nightly/13Copy the code

Add other remote:

nvs remote add iojs https://iojs.org/dist/
nvs remote add chakracore https://nodejs.org/download/chakracore-release/
Copy the code

The alias

An alias is a combination of a remote name and a semantic version. (Do not alias the processor architecture.) When you set the alias, you can omit the remote name, in which case the alias is the default remote. In any other command, you can use an alias instead of a version string.

$NVS alias MyAlias 6.7.0 $NVS alias MyAlias Default /6.7.0 $NVS run myAlias --version v6.7.0 $NVS which myAlias ~ /. NVS/node / 6.7.0 / x64 / bin/node $NVS which myalias 32 ~ /. / NVS/node / 6.7.0 / x86 / bin/nodeCopy the code

Aliases can also refer to local directories, allowing NVS to switch to the local private version of Node.js.

Automatically switch the version based on the directory

In Bash or PowerShell, NVS can automatically switch the node.js version of the current Shell when changing the directory. By default, this feature is disabled. Make it run NVS Auto on. After that, whenever CD or pushd is in a directory that contains a.node-version or.nvmrc file, NVS will automatically switch the Node.js version accordingly and download a new version if necessary. When your CD reaches a directory that does not have a.node-version or.nvmrc file above it, the default (linked) version (if any) will be restored.

~$NVS link 6.9.1 ~/. NVS /default -> ~/. NVS /node/6.9.1/x64 ~$NVS use PATH += ~/. NVS /default/bin ~$NVS auto on ~$CD NVS /default/bin PATH += ~/. NVS /node/4.6.1/x64/bin ~/ myProject $CD.. PATH = ~ /. NVS/node / 4.6.1 / x64 / bin PATH + = ~ /. NVS/default/binCopy the code

The Windows command prompt does not provide this function. Please use PowerShell.

Manual switching.node-version

If your shell is not compatible with auto switching, or if you want to switch manually but still use one of the.node-versionor. NVMRC files, you can either run NVS use with that versionor run NVS auto directly.

$ nvs use auto
Copy the code

The equivalent of

$ nvs auto
Copy the code

How it works

Bootstrapping node

NVS uses less platform-specific shell code, which is bootstrapped by automatically downloading private copies of Node.js. The boot code is just Windows command scripts, Windows Powershell scripts, and dozens of POSIX shell scripts. In addition to the bootloader, shell scripts are also used to export PATH changes to the calling shell (which cannot be executed by a separate Node.js process). However, all the code for querying available versions, downloading and installing Node.js, matching NPM, switching version/architecture/engine, uninstalling, parsing, and updating paths, etc., can be written in JavaScript, and most of it is cross-platform.

Version of the switch

NVS Download the Node.js version in the directory specified by the NVS_HOME environment variable, or in the NVS tool directory not set by NVS_HOME. For example, each build is located in a node/6.7.0/x64 subdirectory based on remote name, semantic version, and architecture.

When you use a version of NVS, the current shell’s PATH is updated to include the bin directory for that version.

Global module

Using NPM install-gornpm link with an NVS installed Node.js in conjunction with it installs the global module or links it to a version-specific directory. (NVS clears any environment variables that NPM_CONFIG_PREFIX may have set.) This means that when NVS switches versions, it is also switching the global set of modules available. The NVS migrate command can migrate these global modules from one Node.js version to another.

— link

The NVS link command creates the symbolic directory link PATH where $NVS_HOME/default points to the specified version (or current version at the time of the command). This is useful when you need to configure fixed paths elsewhere.

On non-Windows platforms, the new shell that provides the source of the nvs.sh script will also set PATH to include the default version if there is a link. On Windows, the PATH environment variable is updated in the user profile, so the new Shell will use the default version.

The NVS ls command lists all local node.js versions using the tag version in the current path > and the tag default (linked) version (if any) #. These can be the same or different. Such as:

Node / 4.5.0 / x64 # node / 4.6.0 / x64 > node / 6.7.0 / x64Copy the code

System linking

If $NVS_HOME is in a directory such as /usr/localor%ProgramFiles%, the NVS link command will also link to the well-known Node.js system location. (This is only allowed if there is no system-installed Node.js yet.)

  • On non-Windows platforms, create symlinks to /usr/local/bin, NPM, and node.js modules that can perform any global installation for Node. Note that after installing or uninstalling the global module that contains the executable, you may need to run the NVS Link again to update the global link. Using NVS to link different versions of Node.js (with different global modules) will update all links accordingly.

  • On Windows, create the symbol directory link %ProgramFiles%\Nodejs and add the directory to the system PATH.

This system link feature is skipped when $NVS_HOME points to a non-system directory, because it is wrong to create symbolic links to user files in the system directory.

dependencies

NVS has no external dependencies other than private copies of nodes that are automatically downloaded. The runtime JS package has very little relevance and is checked in to the repository to avoid NPM install needing it at boot time.