Version management

At present, there are three common NODE version management tools: NVM, NVS and N.

While NVM and NVS are currently cross-platform, N as a node module seems to have some problems with WINDOs.

NVM will also have node download success in some version operations, but the corresponding version of NPM fails to download, I don’t know if it is a special case, I have not encountered.

At present, NVS is developed based on the relevant experience of NVM. The command is relatively simple and clear, but the project auto switch command cannot be persisted. After closing, you have to re-enter it when opening it.

NVM

Node version manager, NVM, runs on a variety of operating systems. Easy to switch and manage Node versions.

Windos

Latest download address: github.com/coreybutler…

And here are four downloadable files.

  • Nvm-noinstall. zip: This is the green installation-free version, but needs to be configured before use
  • Nvm-setup. zip: this is the installation package, after downloading, click install, no configuration to use, convenient.
  • Source code(zip) : zip compressed Source code
  • Sourc code(tar.gz) : tar.gz source code, generally used for * NIx systems

The MAC OS and LINUX

To ensure that first raw.githubusercontent.com/creationix/…

1. Run

The curl - o - https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bashCopy the code

or

Wget - qO - https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bashCopy the code

You are advised to use curl to create a directory ~/. NVM and store the content there.

After the installation is complete, close the terminal and restart the terminal. Enter NVM to verify whether the installation is successful. If Node Version Manager is displayed, the installation is successful.

If command not found: NVM is displayed when you enter NVM on a new terminal, the possible causes are as follows:

Your system may be missing a.bash_profile file. You can create one (via vi or vim), open copy and paste the following code (the last 3 lines of the terminal after a successful NVM installation), save it, and run install again.

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
Copy the code

** Note: ** If you have oh my ZSH installed, you need to add the above configuration information to the.zshrc file, as shown below:

  • You may need to reopen a Terminal window or TAB

If the problem is not solved above, open your.bash_profile file and add the following code: source ~/.bashrc. Save the changes when you are done.

detection

To check whether the installation is successful, we can enter it in the new command window

nvm -v
Copy the code

If the NVM version number and a set of help instructions are displayed, the NVM is successfully installed.

Otherwise, NVM: Command not found may be prompted

use

  • NVM arch [32 | 64] : display the node is run on a 32-bit or 64 – bit mode. Specify 32 or 64 to override the default architecture.

  • NVM install [arch] : This can be the Node.js version or latest stable version. (Optional [ARCH]) Specifies whether to install the 32-bit or 64-bit version (the default is System ARCH). Set [Arch] to all to install the 32 – and 64-bit versions. Adding –insecure at the end of the command bypasses SSL authentication on the remote download server.

  • NVM list [available] : lists installed Versions of Node.js. Optional available, displays a partial list of downloadable versions. This command can be abbreviated to NVM ls [available].

  • NVM on: Enables node.js version management.

  • NVM off: Disables Node.js version management (without uninstalling anything)

  • NVM proxy [URL] : Sets the proxy used for download. Leave [URL] blank to view the current agent. Set [url] to None to remove the proxy.

  • NVM node_mirror [url] : sets the node mirror. The default value is nodejs.org/dist/. I suggest setting…

  • NVM npm_mirror [url] : NPM mirror. The default value is github.com/npm/npm/arc…

  • NVM uninstall: Uninstalls the specified nodeJS version.

  • NVM use [version] [arch] : Switches to using the specified NodeJS version. 32/64 bit [ARCH] can be specified. NVM use will continue to use the selected version, but switch to 32/64-bit mode based on the provided value

  • NVM root [path] : Sets the directory where NVM stores different versions of Node.js. If this directory is not set, the current directory is used.

  • NVM Version: displays the current version of the NVM, which can be abbreviated as NVM V

The command associated

Switching to the Default Version

nvm alias default stable
Copy the code

Default is a default alias, and stable is also the default alias

Changed the default version number of NVM startup to a version number pointing to stable

You can look at NVM LS to see what version numbers these aliases point to

Automatic switch

We usually use the project process, some projects must specify the version, each manual switch is more troublesome, we want to be able to switch automatically. You can use AVN directly for deep integration into your shell and automatically invoke NVM when you change a directory. You can also simply configure it yourself.

bash

~/.bashrc:

cdnvm() { cd "$@"; nvm_path=$(nvm_find_up .nvmrc | tr -d '\n') # If there are no .nvmrc file, use the default nvm version if [[ !  $nvm_path = *[^[:space:]]* ]]; then declare default_version; default_version=$(nvm version default); # If there is no default version, set it to `node` # This will use the latest version on your machine if [[ $default_version == "N/A" ]]; then nvm alias default node; default_version=$(nvm version default); fi # If the current version is not the default version, set it to use the default version if [[ $(nvm current) ! = "$default_version" ]]; then nvm use default; fi elif [[ -s $nvm_path/.nvmrc && -r $nvm_path/.nvmrc ]]; then declare nvm_version nvm_version=$(<"$nvm_path"/.nvmrc) declare locally_resolved_nvm_version # `nvm ls` will check all locally-available versions # If there are multiple matching versions, take the latest one # Remove the `->` and `*` characters and spaces # `locally_resolved_nvm_version` will be `N/A` if no  local versions are found locally_resolved_nvm_version=$(nvm ls --no-colors "$nvm_version" | tail -1 | tr -d '\->*' | tr  -d '[:space:]') # If it is not already installed, install it # `nvm install` will implicitly use the newly-installed version if [[ "$locally_resolved_nvm_version" == "N/A" ]]; then nvm install "$nvm_version"; elif [[ $(nvm current) != "$locally_resolved_nvm_version" ]]; then nvm use "$nvm_version"; fi fi } alias cd='cdnvm' cd $PWDCopy the code

This alias will search for “up” from your current directory to detect.nvMRC files. If it finds one, it switches to that version; If not, it will use the default version.

zsh

If you use ZSH, add the following script to your ~/.zshrc file:

# place this after nvm initialization! autoload -U add-zsh-hook load-nvmrc() { local node_version="$(nvm version)" local nvmrc_path="$(nvm_find_nvmrc)" if [ -n  "$nvmrc_path" ]; then local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")") if [ "$nvmrc_node_version" = "N/A" ]; then nvm install elif [ "$nvmrc_node_version" != "$node_version" ]; then nvm use fi elif [ "$node_version" != "$(nvm version default)" ]; then echo "Reverting to nvm default version" nvm use default fi } add-zsh-hook chpwd load-nvmrc load-nvmrcCopy the code

Save and exit.

Create version control files

Create an.nvmrc file in your project and fill in the version you want to switch:

Then, every time you enter the project directory, NVM will automatically switch to the corresponding node version for you. After you exit the directory, the node version will be restored again.

NVM use is automatically called in directories that contain. NVMRC files

The global module is missing when installing a new version of Node

Because each version of NVM is completely independent, the newly installed version of Node does not automatically download the global modules of the original Node. It can be solved in the following ways:

For example, porting the 10.15.3 global module when installing 12.0.1.

NVM install 12.0.1 - reinstall packages - the from = 10.15.3Copy the code

The MAC OS uninstalls the NVM

  1. Run the following command to remove the NVM content
cd ~/.nvm
rm -rf .nvm
Copy the code

2. Delete NVM configurations in ~/. Profile, ~/.bash_profile, ~/.zshrc, and ~/

supplement

  1. XXX indicates the version number to be switched. In actual operations, XXX can be abbreviated. For example, V10.5.0 can be abbreviated to V10 or even 10. If both 12.22.5 and 12.19.0 exist locally, NVM use 12 only switches to the highest version 12.22.5 by default.

  2. Native command document

We often use not too many commands, but we can use a lot of commands, lazy not to do translation and experience, copy the document.

The command describe
nvm –help Show this message
nvm –version Print out the installed version of nvm
nvm install [-s] <version> Download and install a <version>, [-s] from source. Uses .nvmrc if available
–reinstall-packages-from=<version> When installing, reinstall packages installed in <node/iojs/node version number>
–lts When installing, only select from LTS (long-term support) versions
–lts=<LTS name> When installing, only select from versions for a specific LTS line
–skip-default-packages When installing, skip the default-packages file if it exists
–latest-npm After installing, attempt to upgrade to the latest working npm on the given node version
–no-progress Disable the progress bar on any downloads
nvm uninstall <version> Uninstall a version
nvm uninstall –lts Uninstall using automatic LTS (long-term support) alias lts/*, if available.
nvm uninstall –lts=<LTS name> Uninstall using automatic alias for provided LTS line, if available.
nvm use [–silent] <version> Modify PATH to use <version>. Uses .nvmrc if available
–lts Uses automatic LTS (long-term support) alias lts/*, if available.
–lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm exec [–silent] <version> [<command>] Run <command> on <version>. Uses .nvmrc if available
–lts Uses automatic LTS (long-term support) alias lts/*, if available.
–lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm run [–silent] <version> [<args>] Run node on <version> with <args> as arguments. Uses .nvmrc if available
–lts Uses automatic LTS (long-term support) alias lts/*, if available.
–lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm current Display currently activated version of Node
nvm ls List installed versions
nvm ls <version> List versions matching a given <version>
nvm ls-remote List remote versions available for install
–lts When listing, only show LTS (long-term support) versions
nvm ls-remote <version> List remote versions available for install, matching a given <version>
–lts When listing, only show LTS (long-term support) versions
–lts=<LTS name> When listing, only show versions for a specific LTS line
nvm version <version> Resolve the given description to a single local version
nvm version-remote <version> Resolve the given description to a single remote version
–lts When listing, only select from LTS (long-term support) versions
–lts=<LTS name> When listing, only select from versions for a specific LTS line
nvm deactivate Undo effects of nvm on current shell
nvm alias [<pattern>] Show all aliases beginning with <pattern>
nvm alias <name> <version> Set an alias named <name> pointing to <version>
nvm unalias <name> Deletes the alias named <name>
nvm install-latest-npm Attempt to upgrade to the latest working npm on the current node version
nvm reinstall-packages <version> Reinstall global npm packages contained in <version> to current version
nvm unload Unload nvm from shell
nvm which [current / <version>] Display path to installed node version. Uses .nvmrc if available
nvm cache dir Display path to the cache directory for nvm
nvm cache clear Empty cache directory for nvm

NVS

NVS is a cross-platform utility for switching between different nodeJS versions and branches. NVS itself is written in Node JavaScript.

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

Set up the

The following are the basic Settings. For more details and options on setting up NVS, see the Settings page.

Windos

The Windows Installer (MSI) package is available from the NVS version page on GitHub.

You can also use Chocolatey to install:

choco install nvs
Copy the code

The Mac OS and Linux

Specify the installation path, clone the project, compile and install

Specified path export NVS_HOME = ". $HOME/NVS cloning project "git clone https://github.com/jasongin/nvs" $NVS_HOME "compiler installed." $NVS_HOME/NVS. Sh" installCopy the code

The NVS. Sh script adds an NVS shell function to the environment. The tool can then be invoked directly as NVS without pointing to a path. The install command adds configuration to /.bashrc, /.profile, or ~/.zshrc files so that NVS commands are available in your shell.

Basic usage

Add a node of the latest version:

$ nvs add latest
Copy the code

Or add the latest node LTS version:

$ nvs add lts
Copy the code

Then run the NVS use command to add the node version to the PATH of the current shell:

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

To permanently add it to the PATH, use the NVS link:

$ nvs link lts
Copy the code

Command reference

The command describe
nvs help <command> Get detailed help for commands
nvs install Initialize your configuration file to use NVS
nvs uninstall Remove the NVS from the configuration file and environment
nvs --version Displays the NVS tool version
nvs add [version] Download and decompress the node version
nvs rm <version> Deleting a Node Version
nvs migrate <fromver> [tover] Migrating global modules
nvs upgrade [fromver] Upgrade to the latest major patch
nvs use [version] Use the node version in the current shell
nvs auto [on/off] Automatic switching according to CWD
nvs run <ver> <js> [args...] Run the script using the node version
nvs exec <ver> <exe> [args...] Run the executable using the node version
nvs which [version] Displays the path to the node version binary file
nvs ls [filter] Lists the local node versions
nvs ls-remote [filter] Lists the node versions available for download
nvs link [version] Link a version as the default
nvs unlink [version] Remove the link to the default version
nvs alias [name] [value] Sets or invokes the version alias
nvs remote [name] [value] Sets or calls the download base URI

A version or filter consists of a full or partial semantic version number or version tag (” LTS, “” Latest,” “Argon,” and so on), optionally preceded by a remote name, or optionally followed by a schema, separated by a slash. For example, LTS, 4.6.0, 6/x86, node/6.7/ X64.

See the documentation for more details on each command.

Interactive menu

When called without arguments, NVS displays interactive menus for switching and downloading node versions.

[]

NVS USESconsole-menuThis is a module originally written for this project and then released separately.

VS code support

Visual Studio Code can use NVS to select the version of the node to use during startup or debugging. In launch.json (.vscode is in the project root folder), Add a “runtimeArgs” property with the “runtimeExecutable” property of the NVS version string and a property that references EITHER NVS. CMD (Windows) or NVS (Mac, Linux). (If NVS is not in VS Code’s PATH, you may need to specify an absolute PATH, such as “${env:HOME}/.nvs/ NVS “.)

Example: Configure launch.json so VS Code launches node version 6.10 using NVS:

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

Alternatively, remove the version string from “runtimeArgs” to get the version from the.node-version file in the project directory. For more details, see the NVS VS Code documentation or run NVS Help VS Code.

Configure the remote

The NVS remote command allows you to configure multiple remote download locations. NVS manage versions from different locations separately, so there is no risk of version conflicts. By default, there is a remote that points to the official node.js version:

$ nvs remote
default  node
node  https://nodejs.org/dist/
Copy the code

This makes it possible to get builds from other sources. Add a remote entry for ‘nightly’ builds, list ‘nightly’ builds, and add 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

Support for other remote sources such as:

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 semantic version (processor architecture is not an alias). The remote name may be omitted when you set the alias, in which case the alias refers to 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

Automatic switch

In Bash or PowerShell, NVS can automatically switch node versions in the current shell when you change directories. This function is disabled by default. Let it run NVS Auto on. After that, every time you CD or pushd in a directory that contains a.node-version or.nvmrc file, NVS will automatically switch node versions accordingly and download new versions if necessary. When you CD out where there is no.node-version or.nvmrc file, then the default (linked) version 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

This feature is not available at the Windows command prompt. Use PowerShell instead.

Manual switching.node-version

If your shell is not compatible with auto toggle, or if you prefer manual toggle but still use any.node-version or.nvmrc files, you can either run NVS Auto using that version or run NVS Auto directly.

$ nvs use auto
Copy the code

The equivalent of

$ nvs auto
Copy the code

MAC OS uninstall NVS

  1. Run the following command to remove the NVM content
cd ~/.nvs
rm -rf .nvs
Copy the code

Profile, ~/.bash_profile, ~/.zshrc, and ~/.bashrc files

N modules

There is also a node version management tool very useful, the N module, which is an NPM package.

Install and use

  1. First install the N module:

     npm install -g n
    Copy the code
  2. Step 2: Upgrade Node.js to the latest stable version

     n stable
    Copy the code

N can also be followed by + version number for example:

N V8.9.1 or n V9.2.0Copy the code

Why not recommend the simpler n module

Linux and MAC users ignored

  1. Installation error, forced installation does not work

  2. Why is that?

    N Not compatible with Windows! , n not compatible with Windows! N not compatible with Windows!!

Warehouse source management – NRM

We solved the problem of multiple version switching, but for some reasons, we often need to switch the warehouse source address, such as NPM official source address and private NPM warehouse address, Taobao source and so on. Each use:

npm config set registry URL
Copy the code

There seems to be some trouble. NRM module helps you solve problems:

  1. The installation

    npm install -g nrm
    Copy the code
  2. use

    NRM ls // View available addressesCopy the code

    NRM use XXX // Switch to a source, for example, NRM use CNPM Switch to the source ADDRESS of CNPMCopy the code

    NRM add <name> < URL > Adds a source addressCopy the code

When we use NPM install and other commands to install the package, we actually access the source address we set.

Automatic version change – AVN

In the previous NVS and NVM sections, we introduced automatic version switching by modifying scripts, but there is a simpler module to use.

Supports switching between NVM and N. However, it is different from the above modification command in that the above method only uses the specified version when entering the corresponding directory, and restores the default version when exiting the directory. This method enters the corresponding directory, switches the corresponding version, but exits the directory, the version is still the same, only when the shell window restarts to restore the default.

However, after testing under ZSH + NVM + Node (14.x.x), this module has compatibility problems at present, that is, the main version of this module you install must be 10.x.x, which dependency may have problems with the higher version, I will find out later.

But if you use NVM for version switching, you can solve the problem with the exec command. N There should also be methods corresponding to the module downloaded for the specified version.

Official installation (major version higher than 10 will cause problems, not fixed as of 2021.10.31)

npm install -g avn avn-nvm avn-n
avn setup
Copy the code

With the help of NVM

Install a 10.x.x version locally using NVM. To my 10.15.3

NVM exec 10.15.3 NPM install -g avn avn-nVM avn-n NVM exec 10.15.3 AVn setupCopy the code

use

Hit the. Node-version file in the project directory and fill in the required node version.

Now, when you CD into a directory with a.node-version file, AVN will automatically detect the change and switch to that version of Node using your installed version manager.

Plug-in explain

Avn supports the following version managers:

NVM through AVN-nVM N Through AVN-n Nodebrew Through AVN-nodebrewCopy the code

Afterword.

There are a lot of details and in view of my current level of limitations, the article can not be foolproof and follow the development of new technology, if there is any mistake, please blame and reply to me, grow together.

Later, I plan to write an NPM related article, which will be combined with this article.

Welcome to visit my official website DLLCNX