Node.js environment installation in Ubuntu (WSL)

Node.js environment installation

Node.js environment installation can be installed with nvm or fnm version manager, which is beneficial to the use of multiple Node versions.

1 NVM installation

1.1 Github installation method of NVM

The github address of nvm: https://github.com/creationix/nvm

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

However, limited by the speed of foreign networks, the above-mentioned official installation methods often have slow download speeds or cannot be downloaded. Therefore, the following domestic code cloud installation methods can be used.

1.2 Gitee (code cloud) installation of NVM

Gitee (code cloud) will pull some common project source code libraries on Github to its own website, so you can find the source code libraries you need on the Gitee speed download website.

1.2.1 NVM source code download

Gitee fast download website: https://gitee.com/mirrors
insert image description here
insert image description here
Find the corresponding address in the searched nvm code library, and use the Git command to download the source code library.
insert image description here

insert image description here```
Use the git clone command to download the code to the .nvm folder in the user directory.

git clone https://gitee.com/mirrors/nvm.git .nvm

Cloning into '.nvm'...
remote: Enumerating objects: 8693, done.
remote: Counting objects: 100% (382/382), done.
remote: Compressing objects: 100% (358/358), done.
remote: Total 8693 (delta 240), reused 15 (delta 15), pack-reused 8311
Receiving objects: 100% (8693/8693), 2.58 MiB | 587.00 KiB/s, done.
Resolving deltas: 100% (5595/5595), done.

1.2.2 nvm installation

After entering the .nvm directory, install and update nvm through the bash install.sh command.

(base) zhou@ZHOU-PC:~$ cd .nvm
(base) zhou@ZHOU-PC:~/.nvm$ bash install.sh
=> nvm is already installed in /home/zhou/.nvm, trying to update using git
=> * (HEAD detached at FETCH_HEAD)
  master
=> Compressing and cleaning up git repository

=> Appending nvm source string to /home/zhou/.bashrc
=> Appending bash_completion source string to /home/zhou/.bashrc
/usr/bin/env: ‘bash\r’: No such file or directory
=> Close and reopen your terminal to start using nvm or run the following to use it now:

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

The above command will also add the NVM_DIR environment variable to .bashrc at the same time, and make the command take effect through source ~/.bashrc , which will be used later for related configuration.
insert image description here

1.3 NVM domestic source settings

Use nvm -v to check the nvm version number to confirm that nvm has been installed correctly.

nvm -v
0.39.1

Then you can use NVM to install the Node.js environment.

1.3.1 Configuration of nvm installation source in nvm.sh file

However, when NVM installs Node, it still uses a foreign website, and the download speed is relatively slow. The installation source configuration is located in nvm.sh** under the **.nvm directory . Regarding the above two environment configurations, you can see that the installation source is located at https://nodejs.org/dist .

# nvm.sh中的环境变量设置
nvm_get_mirror() {
    
    
  case "${1}-${2}" in
    node-std) nvm_echo "${NVM_NODEJS_ORG_MIRROR:-https://nodejs.org/dist}" ;;
    iojs-std) nvm_echo "${NVM_IOJS_ORG_MIRROR:-https://iojs.org/dist}" ;;
    *)
      nvm_err 'unknown type of node.js or io.js release'
      return 1
    ;;
  esac
}

insert image description here
At this time, you can temporarily modify the NVM installation source to a domestic source (npmmirror China Mirror Station, https://npmmirror.com/ )
. The Taobao NPM website that was often used before has been revised to npmmirror China Mirror Station, and the mirror source behind it has also been changed to npmmirror China. mirror site.

1.3.2 Temporarily set nvm installation source

Enter the following command in the shell to temporarily set the npmmirror China installation source of NVM:

export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node
export NVM_IOJS_ORG_MIRROR=https://npmmirror.com/mirrors/iojs

This command is only valid when the current terminal environment is enabled, and then you can install Nodejs according to the ∗ ∗ command ∗ ∗ nvminstall − − lts ∗ ∗ in section 1.3. After the terminal is reopened, it becomes invalid. If you want to continue to use it, you can re-enter this command. 3.3 Permanently set the nvm installation source (recommended) Edit the .bashrc file in the terminal, first use the command ∗ ∗ sudovi / .bashrc ∗ ∗ to open .bashrc, and add the following command in it, note that the location where the command is added is ∗ ∗ export NVMDIR = " Section 1.3 **command **nvm install --lts** installed Nodejs. The terminal will fail after re-opening. If you want to continue to use it after failure, you can re-enter this command. 3.3 Permanently set the nvm installation source (recommended) Edit in the terminal. bashrc file, first use the command **sudo vi ~/.bashrc** to open .bashrc, and add the following command in it, note that the location where the command is added is **export NVM_DIR="1. Section 3 _commandnvminstallltsNodejs is installed . _ _ _ _ _ After the terminal is reopened , it becomes invalid . If you want to continue to use it , you can re- enter this command . 3.3 Permanently set the n v m installation source ( recommended ) in the finalend editor .b a s h r c file , _First use the commandsudovi /.bashrcOpen .b a s h r c and add the following command in it , note that the location where the command is added is inexportNVMDI R=Under " HOME/.nvm":

export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node
export NVM_IOJS_ORG_MIRROR=https://npmmirror.com/mirrors/iojs

insert image description here

Save and exit, and use the command source ~/.bashrc to make the above changes take effect.

1.4 Node.js installation

At this time, you can use domestic sources to install Node.js, and directly use nvm install --lts to install the long-term support environment (latest) of Node.js, including node and npm. If you want to choose the version of the Node.js environment you want to install, you can query the long-term support version that can be installed online through node ls-remote --lts , and then install the specified long-term support version.

# 查询可以安装的长期支持版本
nvm ls-remote --lts

	   .......
       v14.17.4   (LTS: Fermium)
       v14.17.5   (LTS: Fermium)
       v14.17.6   (LTS: Fermium)
       v14.18.0   (LTS: Fermium)
       v14.18.1   (LTS: Fermium)
       v14.18.2   (LTS: Fermium)
       v14.18.3   (LTS: Fermium)
       v14.19.0   (Latest LTS: Fermium)
       v16.13.0   (LTS: Gallium)
       v16.13.1   (LTS: Gallium)
       v16.13.2   (LTS: Gallium)
       v16.14.0   (Latest LTS: Gallium)
nvm install --lts

Installing latest LTS version.
Downloading and installing node v16.14.0...
Downloading https://npmmirror.com/mirrors/node/v16.14.0/node-v16.14.0-linux-x64.tar.xz...
###################################################################################### 100.0%
###################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Creating default alias: default -> lts/* (-> v16.14.0)

After installing the Node.js environment, if there are multiple Node.js environments, you can specify which version of the Node.js environment to use by means of nvm use.

nvm use --lts
Now using node v16.14.0 (npm v8.3.1)

1.5 View the currently used Node and NPM versions

In the case of multiple Node.js version environments, you can view the currently used node and npm versions through the node -v and npm -v commands.

node -v
v16.14.0

npm -v
8.3.1

nvm -v
0.39.1

2 fnm install

Fast Node Manager (fnm) is one of the Node version managers. Built-in Rust, a fast and convenient management tool for Node.js multi-release versions. fnm can cross-version support macOS, Windows and Linux, and a series of derivative commands (Bash, Zsh, Fish, PowerShell, Windows command line), it compiles at "brain speed", and supports .node-version and .nvmrc files for compatibility .

2.1 Script installation method of fnm

fnm official installation address : https://nodejs.org/zh-cn/download/package-manager/#fnm
fnm Github address : https://github.com/Schniz/fnm

Install fnm using the following command

curl -fsSL https://fnm.vercel.app/install | bash

For many well-known reasons, the download speed of github is slow, which often leads to unsuccessful installation.

2.2 The source code installation method of fnm

2.2.1 fnm source code download

https://github.com/Schniz/fnm/releases Download the required source code
insert image description here

After the source code is downloaded, decompress it, create a new file directory **.fnm** under the user's Home directory, and copy the decompressed source code file to the .fnm folder

zhou@ZHOU-PC:~/.fnm$ ls -la
total 6872
drwxr-xr-x  2 zhou zhou    4096 Apr 26 22:05 .
drwxr-xr-x 11 zhou zhou    4096 Apr 26 22:05 ..
-rwxr--r--  1 zhou zhou 7028576 Feb 16 08:21 fnm

2.2.2 Add the corresponding configuration in the .bashrc file

 # fnm
  export PATH=/home/zhou/.fnm:$PATH
  eval "`fnm env`"

Execute the source ~/.bashrc command or close the window and reopen a new window to enable the above configuration.

2.2.3 Check whether fnm can be used normally

insert image description here

2.3 Set fnm installation source as domestic source

2.3.1 View fnm installation source

zhou@ZHOU-PC:~$ fnm env
export PATH="/home/zhou/.local/state/fnm_multishells/597_1650983068281/bin":$PATH
export FNM_MULTISHELL_PATH="/home/zhou/.local/state/fnm_multishells/597_1650983068281"
export FNM_VERSION_FILE_STRATEGY="local"
export FNM_DIR="/home/zhou/.fnm"
export FNM_LOGLEVEL="info"
export FNM_NODE_DIST_MIRROR="https://nodejs.org/dist"
export FNM_ARCH="x64"

insert image description here

2.3.2 Change fnm installation source to domestic source

Add the corresponding configuration in the .bashrc file, select one of the following three configurations

# 清华大学开源镜像站
export FNM_NODE_DIST_MIRROR=https://mirrors.tuna.tsinghua.edu.cn/nodejs-release/

# npmmirror 中国镜像站
export FNM_NODE_DIST_MIRROR=https://npmmirror.com/mirrors/node

# npm腾讯镜像站
export FNM_NODE_DIST_MIRROR=https://npmmirror.com/mirrors/node

insert image description here
Execute the source ~/.bashrc command or close the window and reopen a new window to enable the above configuration.

Check again whether the fnm installation source has been changed to a domestic source
insert image description here

2.4 Node.js installation

# 安装最新的长期支持版本
zhou@ZHOU-PC:~$ fnm install --lts
Installing Node v16.14.2 (x64)
#还可以在安装之前先查询可以安装的版本
fnm list-remote

After the installation is complete, a node-versions** file will be generated in the **.fnm directory , which contains the latest downloaded long-term support version or the specified version of Node.js.
insert image description here

3 Set the npm domestic source in Nodejs

The default npm installation package still uses foreign official sources, and the network speed is worrying, so you can also change the source to the domestic npmmirror China mirror station or npm Tencent mirror station.

npm国内源(npmmirror 中国镜像站,二选一)
npm config set registry https://registry.npmmirror.com/

npm国内源(npm腾讯镜像站 二选一)
npm config set registry http://mirrors.cloud.tencent.com/npm/

The above command will add the following statement to the .npmrc file in the user directory :

registry=https://registry.npmmirror.com/
registry=http://mirrors.cloud.tencent.com/npm/

4 Install pnpm and set pnpm as domestic source (optional)

pnpm only adds the direct dependencies of the project to the root directory of node_modules by using symbolic links, so that the source code can access the dependent packages that are not set by the current project.

# pnpm安装
npm install -g pnpm
added 1 package in 902msadded 1 package in 902ms
# pnpm 设置国内源
pnpm config set registry https://registry.npmmirror.com/

The above command will add the following statement to the .npmrc file in the user directory :

registry=https://registry.npmmirror.com/

5 npmmirror Chinese mirror site

npmmirror Chinese mirror station

开源镜像: https://npmmirror.com/mirrors/
Node.js 镜像: https://npmmirror.com/mirrors/node/
alinode 镜像: https://npmmirror.com/mirrors/alinode/
ChromeDriver 镜像: https://npmmirror.com/mirrors/chromedriver/
OperaDriver 镜像: https://npmmirror.com/mirrors/operadriver/
Selenium 镜像: https://npmmirror.com/mirrors/selenium/
electron 镜像: https://npmmirror.com/mirrors/electron/

Guess you like

Origin blog.csdn.net/gandongusa/article/details/123010941