banner
biuaxia

biuaxia

"万物皆有裂痕,那是光进来的地方。"
github
bilibili
tg_channel

【Repost】Node.js and npm and yarn

title: [Repost] nodejs and npm and yarn
date: 2022-06-06 11:09:00
toc: false
index_img: https://puep.qpic.cn/coral/Q3auHgzwzM4fgQ41VTF2rILyI8ckeUFuZTtuo8Y2TUQ8JpxuBcS3aQ/0
category:

  • Frontend
    tags:
  • yarn
  • nodejs
  • npm
  • global
  • add
  • remove
  • dir
  • config
  • list
  • registry

Original article: nodejs and npm and yarn - 掘金, this site is only for saving records and adjusting formatting, and does not engage in profit-making commercial activities.

1. What is nodejs#

Node.js is an open-source, cross-platform JavaScript runtime environment that allows JavaScript to run on the server side. Most of the basic modules in Node.js are written in JavaScript. Before the emergence of Node.js, JavaScript was usually used as a client-side programming language, and programs written in JavaScript often run on users' browsers. In simple terms, Node.js is JavaScript that can run on the server side.

So, in simple terms, node.js is an IDE (Integrated Development Environment) for server-side JavaScript, abbreviated as Node environment.

2. What is npm#

npm (short for Node Package Manager) is the default software package management system for Node.js, written in JavaScript.

For more information about the emergence and development of npm, I recommend reading Fang Fang's answer. Please visit What is npm? (Not a tutorial)

Every language has its own package management tool, and npm is the package management tool for the JavaScript language. Server-side JavaScript usually uses NPM as a dependency management tool. Although npm is translated as the management of node.js packages, it is ultimately a package management tool for the JavaScript language. Since frontend development is mainly based on the JavaScript language, we also use many JavaScript code packages written by others, such as jQuery.js, vue.js, etc. Therefore, npm has become an excellent JavaScript package management tool that must be understood and mastered in learning frontend development.

3. Installation of node#

Some people may think, can't I just install npm without using node.js since I only do frontend development and not backend development?

Yes, you can, but node.js and npm work together and cannot be separated from each other. npm comes with node.js, so when you install node.js, npm is automatically included. Moreover, node.js is currently very popular. If you want to enter the frontend field, how can you guarantee that you will not use node.js at some point? Therefore, it is recommended to install node.js to use npm.

Download and install a single version from the official website#

Node.js official website

It is recommended to choose an even-numbered version, such as Node8, Node10, Node12, as they are stable versions. Choose to download the .msi installation file. x64 is for 64-bit systems, and x86 is for 32-bit systems. The installation steps are as follows:

image

image

image

image

After the download is complete, click on the installation. It is recommended to modify the installation directory because the installation path of node.js does not allow spaces. In the options selection, make sure to select all options, especially the "Add to PATH" option. However, it is selected by default, so you can just click "Next" to proceed. After the installation is complete, run the command node --version to check the current version. If it is successful, you are good to go. If not, check if the environment variables are configured correctly.

Using NVM version management tool#

In addition to downloading and installing node.js from the official website, you can also use the NVM tool to control the version switching of node.js. For example, I recently needed to upgrade node.js and used the recommended n module management tool found online, but it was useless and reportedly not supported on Windows. Then I used the NVM management tool, which is really useful.

NVM is a version management tool for node.js that provides node.js download, installation, and version switching functions.

1. Installing NVM#

Download link for Windows version: nvm-windows

Go to the link and find nvm-setup.zip. After downloading, you will get an .exe installation program. During the installation, you will encounter two directory selections. The first one is the installation path of the nvm tool, and the second one is the path of the currently used node.js version. You can set them as you like, as shown below:

First directory selection

image

Second directory selection

image

After installing NVM, run nvm --version to check if the version number and command information are displayed, indicating a successful installation.

image

2. Switching NVM download source#

After installing nvm, don't rush to download node.js because the default download source for node.js and npm is overseas, which is very slow. Therefore, you need to change the default download source of nvm first. Run the following commands:

nvm node_mirror https://npm.taobao.org/mirrors/node/
nvm npm_mirror https://npm.taobao.org/mirrors/npm/

3. Start using nvm#

Now you can start installing node.js. Use the command nvm install <version> [arch], where version is the version number and arch is an optional parameter that specifies the operating system architecture. The default is 64-bit. If you are using a 32-bit operating system, set arch to 32. These commands are explained when nvm --version is displayed, so you can take a look.

nvm install latest             // Install the latest version of node
nvm install version            // For 64-bit operating systems
nvm list                       // View all installed node versions
nvm use version                // Switch versions

Not sure which version numbers are available for installation? Check here: nodejs-realease

4. Usage of npm#

Modifying the default download source#

After installing node.js, enter the following command in the command line to check if npm is included:

image

Before using npm to install or download various packages, please note that the default download source of npm is overseas, and the download speed is extremely slow. Therefore, you need to set the download source to Taobao, a popular Chinese e-commerce website. To do this, you need to use the download source management tool for npm called nrm.

  1. Install nrm with npm by running the following command:
  2. npm i -g nrm // The first time may be slow as nrm needs to be downloaded from overseas
  3. After the download is complete, run nrm --version to check if the version number is displayed, indicating a successful installation
  4. Run nrm ls to view the available download sources. The one with a * is the currently used source, which is npm by default
  5. Use nrm use taobao to select Taobao as the download source

image

npm installation commands#

Global installation:

npm i -g package-name

or

npm install -g package-name

Note: i is the abbreviation of install, and g is the abbreviation of global

Global uninstallation:

npm uninstall -g package-name

Where are the globally installed packages with npm?

C:\Users\Administrator\AppData\Roaming\npm\

The location may vary. You can use the command which package-name to check the location, for example:

image

You can also use the following command to check the location, which is in the node_modules folder:

npm config get prefix

5. Installation of yarn#

Yarn installation#

Yarn is a new package management tool for the JavaScript language. It can be used instead of npm and is faster, more stable, and easier to use. The installation method is as follows:

Yarn official website

On the website, you don't need to choose a version. It recommends a version based on your computer's operating system. Simply click on the .msi installation file to download and install it. During the installation, it is recommended to modify the installation path and avoid spaces in the path.

image

After installing Yarn, run yarn --version to check if it is installed successfully.

Yarn detects the presence of npm and automatically sets the download source to be the same as npm because npm has already been installed and configured with the default download source.

Use yarn config list to view the yarn-related configurations and download source registry.

Use yarn config get registry to check the current download source.

If it is not the same as npm, you can install the corresponding source management tool for yarn, which is yrm, similar to npm's nrm.

yarn global add yrm

Then use yrm use taobao

Yarn commands#

1. Global installation#

yarn global add package-name

2. Global uninstallation#

yarn global remove package-name

3. View global installation directory#

yarn global dir

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.