[Nodejs] Node.js development environment installation

insert image description here

1. Version introduction


Enter node -v in the command window to view the version

  • 0.x is not technical ES6 at all
  • 4.x partially supports ES6 features
  • 5.x partially supports ES6 features (more than 4.x), which is a transitional product. Now there should be no reason to use this
  • 6.x supports 98% of ES6 features
  • 8.x supports ES6 features

2. Node.js runtime environment configuration: through the Node.js installation package (not recommended)


Go to the official website of Node.js to download the installation package:

image-20221102141924455

We can also download historical versions .
insert image description here

If you need to install other versions later, you can do this: re-download the latest installation package and overwrite the installation.

However, we do not recommend directly using the Node.js.msi (windows) or Node.js.pkg (Mac) installation package for installation, because the following problems will occur.

Problems caused by installing packages through Node.js:

  • When installing a new version, the previous version needs to be overwritten; and many global toolkits installed in previous versions need to be reinstalled.
  • Unable to roll back to an older version.
  • Can't switch between multiple versions (Many times, different projects need to use a specific version. Or, I want to temporarily try a new version's features)

Therefore, we don't need to install Node.js for the time being, and we will install Node.js with NVM later. Through NVM, multiple versions of Node.js can coexist and switch flexibly.

Node.js 版本常识
- 偶数版本为稳定版(0.6.x ,0.8.x ,8.10.x)
- 奇数版本为非稳定版(0.7.x ,0.9.x ,9.11.x)
- LTS(Long Term Support)

参考链接:[node.js 中 LTS 和 Current 的区别](https://blog.csdn.net/u012532033/article/details/73332099)

3. Node.js runtime environment installation: via NVM (recommended)


NVM: node.js version manager, used to manage the version of node.

We can install NVM first, and then install Node.js through NVM. This is the officially recommended practice.

The steps for Windows installed Node.js are as follows.

3.1 Install NVM:

(1) Let's download the NVM installation package :

image-20221102142420968
After downloading, unzip it directly to the D:\web directory:
insert image description here

(2) In the above directory, create a new settings.txt file, and fill in the contents as follows:

root: D:\web\nvm
path: D:\web\nodejs
arch: 64
proxy

Explanation of the above content:

  • The root configuration is: the directory where the current nvm.exe is located
  • The path configuration is: the directory where the node shortcut is located
  • arch is configured as: the bitness of the current operating system (32/64)
  • proxy does not need to be configured

(3) Configure environment variables:

  • NVM_HOME = D:\web\nvm (the current directory where nvm.exe is located)
  • NVM_SYMLINK = D:\web\nodejs (the directory where the node shortcut is located)
  • PATH += ;%NVM_HOME%;%NVM_SYMLINK%

After the configuration is successful, restart the resource manager.

3.2 Verification

(1) Enter the nvm command to check whether the environment variable is configured successfully

(2) Enter nvm ls to view all installed node versions.

(3) Enter nvm -v to view the installed nvm version.

(4) Enter node -v to view the version of node being used.

If Node installation fails, you can refer to the link above.

3.3 Install the specified version of Node.js

nvm install <版本号>
#举例
nvm install 8.10.0

Enter node -v to view the currently used node version.

For common commands of NVM, see the next paragraph for details.

Supplement:
If the Node installation fails, you can add the following two lines to the settings.txt file above to modify the mirror source:

node_mirror: https://npmmirror.com/mirrors/node/
npm_mirror: https://npmmirror.com/mirrors/npm/

Reference link: Install npm, nvm, node

4. Common commands of NVM


Note that this paragraph refers to the common commands of NVM, not the common commands of Node.

Check the current nvm version:

nvm --version

View all Node.js versions installed locally:

# 方式1
nvm ls

# 方式2
nvm list

Install the specified version of Node.js:

nvm install 版本号
# 举例
nvm install 8.10.0

Uninstall the specified version of Node.js:

nvm uninstall <版本号>

Switch to use the specified version of node:

nvm use version number
Set the default version of node:

nvm alias default <版本号>

View the installation path of the global npm package:

npm root -g
查看远程服务器端的所有 Node 版本:
nvm ls-remote

After executing the above command, in the listed version list, any version marked with Latest LTS indicates that it is a long-term maintenance version. When we install, it is recommended to install these versions. Of course, we can also check the historical version of LTS at the URL .

5.Common commands of Node.js


Check the version of node:

$ node -v

Execute script string:

$ node -e 'console.log("Hello World")'

Run the script file:

$ node index.js
$ node path/index.js
$ node path/index

View help:

$ node --help

Guess you like

Origin blog.csdn.net/weixin_43094619/article/details/131897758