The relationship between node.js, npm, and nvm is well explained in the cover image. For a more in-depth understanding of the concepts and commonly used commands, please see the article details.
Relationship between the Three#
-
node.js is JavaScript that runs on the server side. It is based on Google's V8 engine and executes JavaScript very quickly with good performance.
-
npm is the package management tool for node.js. It does not need to be installed separately as it comes with the installation of node.js. Their relationship can be compared to pip in Python.
-
nvm, short for nodejs version management, is a version management tool for node.js. It allows you to install and switch between different versions of node.js.
hexo-douban is a third-party package that can crawl data from Douban. If the node.js version is too high, it may not be able to crawl the data, so you need to switch to a lower version of node.js, such as v12.18.0.
Uninstalling and reinstalling node.js is not only very troublesome, but it may also cause incompatibility issues when installing other third-party packages. Reinstalling repeatedly is not a good solution, and this is where nvm comes in handy.
Installation Recommendations#
Install nvm first, then install node.js
-
Download and install directly from the nvm installation address. Just click "continue" all the way. The installation package is very small, and the default path is fine.
-
In the command window, use the command
nvm install <version>
to install the specified version according to the actual situation. Useversion = latest
to install the latest stable version. -
Use the command
npm install <package>
to install the extension package you need.
Common Commands#
Common nvm commands:#
nvm list available
- Displays a partial list of available versions for download.nvm list
- View the installed versions on the computer.nvm use
- Use the specified version of node.js.- If the download of node is slow, please change the mirror source to a domestic one:
nvm node_mirror https://npm.taobao.org/mirrors/node/
- If the download of third-party packages is slow, please change the mirror source to a domestic one:
nvm node_mirror https://npm.taobao.org/mirrors/npm/
Other nvm commands:#
Common Issues:#
npm is not installed when installing nodejs with nvm#
After installing a new node using the NVM management tool, it is found that npm is not available for use.
When using NVM to install node, npm is not installed by default, so we need to download it ourselves and place it in the corresponding node directory of NVM.
Download link for npm: http://npm.taobao.org/mirrors/npm/ (Download the zip file for the corresponding version)
Node version corresponding to npm version: https://nodejs.org/zh-cn/download/releases/
After downloading, extract the zip file to the node_modules directory of the corresponding node version in the NVM installation directory.
I extracted it to "C:\Users\cyc\AppData\Roaming\nvm\v12.22.0\node_modules".
Then rename the extracted folder to npm.
Finally, copy the npm and npm.cmd files in the bin directory of the npm folder to the root directory of the corresponding node version, as shown in the image: