Learn about two powerful Node package managers: npm and yarn
This article will take you through the two powerful package managers of Node.js: npm and yarn. I hope it will be helpful to you!

The first step to learn Node is to understand node’s package manager: npm, I believe everyone is familiar with npm, because we often use it to download some package resources
, but because of npm’s resource library (https: //www.npmjs.com/) In foreign countries, the speed of using it to download resources is relatively slow, so yarnthesethird-partynode package managers# appeared. ##And the domestic Taobao mirror (cnpm) that is updated synchronously with the npm warehouse
The Node series column has started to be updated. Follow the blogger, subscribe to the column, and learn Node without getting lost!
1. Use of npm
What is npm
Use## Before #npm, you must first understand what npm is, mentioned in the first article of the Node series column [Node.js | The only way from front-end to full stack] npm is the open source warehouse of Node, and is the largest open source warehouse in the world.
As of March 17, 2020,npm
provided 1.3 million packages to approximately 12 million developers who These software packages are downloaded 75 billion times every month If you want to download and use the resources in the
warehouse, you can use the npm command ( npm, such as npm i axios download axios) or use other third-party instructions (third-party Node package manager ), such as yarn, etc.
npmIn NodeJSis the package management and distribution tool for
NodeJSPackage managementis reflected in that it is a warehouse of NodeJS, which stores and manages various software packages of
NodeJSDistribution toolin our configurationis reflected in using the npm command to download the package in the
npmwarehouse
environment, npm command module is installed together with NodeJS. We can run npm -v through the terminal to view the installed version:
But if the
version installed by default is too old, you can also manually install and update npm: npm i npm@latest -g
@latestnpmrepresents installing the latest version,
A magical thing can be found above, we are installing-grepresents global installation, thesenpminstructions will be discussed later
through npm, install ourselves? This is actually easy to understand.
is also stored as a package in the npm warehouse, and the name of this package is npm, see npm Address: https://www.npmjs.com/package/npm
So
But in fact, the word
npmrefers to the
npm command Modulealso refers to thenpmNodeJSopen source warehouse itself, so we have it innpm
(this npm represents NodeJS Open source warehouse) Downloadnpm(This npm represents the package named npm, this package is the instruction module of npm)
npm common instructions
npmhas many instructions. Here are only the commonly used ones. For more information, please see the official npm documentation
- npm init
: Generate
package.json
##npm install - :
Download
all resources recorded inpackage.json npm install package Name - :
Download
specified package to the current directorynpm uninstall Package name - :
Uninstall
current The package specified in the directory npm update package name: UpdateThe specified package in the current directory. If no package name is added, all packages in the current directory will be updatednpm outdated package name: Check Whether the specified package in the current directory is outdated, if no package name is added, all packages in the current directory will be checkednpm info package name:Getdetailed information of the package in the current directorynpm list
:View all packages installed in the current directory and their dependencies and display the version number (list can Abbreviated asls)- npm list package name
:
Viewspecified package installed in the current directory The version number (list can be abbreviated as ls) A few additions:
- install
- can be abbreviated to
i
, for example:npm install axioscan be abbreviated tonpm i axios uninstall - can be abbreviated as
un
Add @ after the package name The - symbol can specify the version of the package, such as:
npm i md5@1
Download version 1 of md5,npm i md5@latestmeans download the latest version of md5
Command suffix
- -g
: Specify
npmGlobal environmentThe default command is to operate in the current directory, adding
#--save-g
is specified in theglobal environmentOperation, install the latest version of npm globally as mentioned above:npm i npm@latest -g, so that npm## can be used in anydirectory can be abbreviated as - -s
: Specify the dependencies under
--saveproduction environment( Recorded independencies) After thenpm5version, the default is, if installed in both the production environment and the development environment Axios needed:
can be abbreviated asnpm i axios -s
##--save-dev -D -
: Specify the dependencies under development environment
(recorded indevDependencies)If installed in the production environment, it is not required Babel used (only used in development environment): npm i babel -D
can be abbreviated as
--save-prod -P -
: the same as --save
can be abbreviated as--save -optional-O : Specify optional dependencies
(recorded inoptionalDependencies)--no-save: Will not be recorded in - package.json
, please see my article: The difference between npm install -g/–save/–save-devFor the specific functions and differences of-g, --save, --save-dev
The npm command suffix can also be placed in front of the package name:
npm i -g npm@latest
Dependencies Package management
In npm, the well-known dependencies are: dependencies and
devDependencies In addition, there are actually: peerDependencies
- optionalDependencies
- package.json
:
We mentioned these types of dependencies when we talked about the npm directive suffix

dependenciesanddevDependencies
You can check out my other article: The difference between npm install -g/–save/–save-dev
peerDependencies
You can view the article by the boss: Understanding peerDependencies in one article
optionalDependencies
Optional dependencies, if there are some dependent packages that can still run even if the installation fails or you want npm to continue running, you can use optionalDependencies, and optionalDependencies
dependencies, so don’t write
##bundledDependencies/bundleDependencies
Packaging dependencies, bundledDependencies is an array object containing the name of the dependent package. When publishing, the packages in this object will be packaged into In the final release package, the packages in the array must first be declared in devDependencies or
, otherwise the package will report an error
package.json中需要注意的包版本问题
通过npm下载的所有包的版本信息都会记录在package.json中
在运行npm i时就会根据package.json中记录的包信息进行下载,它的下载规则如下:
-
包版本以
^开头时(默认情况),会锁定大版本// package.json "dependencies": { "md5": "^2.1.0" // ^开头的 },通过
npm i将会安装md52.x.x的最新版本(2大版本下的最新版本),并不一定是2.1.0,还可能是2.3.0 -
包版本以
~开头时,会锁定到第二个大版本// package.json "dependencies": { "md5": "~2.1.0" },通过
npm i将会安装md52.1.x的最新版本(2.1版本下的最新版本),并不一定是2.1.0,还可能是2.1.1 -
包版本为
*,会锁定到最新版本// package.json "dependencies": { "md5": "*" },通过
npm i将会安装md5的最新版本 -
包版本前不带前缀,会锁定到指定版本
// package.json "dependencies": { "md5": "2.1.0" },通过
npm i将会安装md5的2.1.0版本
解决npm速度慢的问题
因为npm仓库在国外,我们在国内使用npm指令下载这个国外仓库的内容速度会比较慢
这时我们就可以运行以下指令将npm的仓库源切换到国内的淘宝镜像(cnpm) 的源:
npm config set registry https://registry.npmmirror.com
使用npm config get registry查看当前源:

往后再使用npm时就会自动从国内的淘宝镜像仓库下载了,速度就会很快
淘宝镜像之前的源地址为http://registry.npm.taobao.org,现在更改为了http://registry.npmmirror.com,查看详情
但我们这样通过修改npm的配置进行源的切换难免会有点麻烦,我们可以全局安装一个nrm来帮助我们快速的切换npm源
使用nrm快速切换npm源
全局安装nrm:
npm install -g nrm
执行nrm ls可查看可切换的npm源:

使用npm use 切换源,如切换到淘宝源:nrm use taobao

使用nrm test 源名测试相应源的响应时间:

可以看到淘宝源的响应速度要比npm的默认源快很多
中国npm镜像:cnpm
cnpm是一个完整的npmjs.org镜像,可以用它代替官方版本
cnpm与官方版本的同步频率为10分钟一次,cnpm官网
下载cnpm:
npm install -g cnpm --registry=https://registry.npmmirror.com
cnpm就是淘宝镜像,上面我们使用淘宝镜像源只是将npm的源更改为淘宝镜像(cnpm)的源(这个源其实就是指仓库的地址),之后还是通过npm指令进行使用
而这里是直接下载cnpm这个完整镜像,之后就可以使用cnpm指令而不是npm指令:
cnpm installcnpm i axios -g // ....
cnpm的指令与npm的指令完全相同,使用时直接使用cnpm代替npm就行
二、yarn的使用
yarn是Facebook发布的一款依赖管理工具,它比npm更快、更高效
安装:
npm install -g yarn
更新yarn:
yarn set version latest yarn set version from sources
优点
速度超快
yarn缓存了每个下载过的包,所以再次使用时无需重复下载。 同时利用并行下载以最大化资源利用率,因此安装速度更快Super safe
Before executing the code,yarnwill verify the integrity of each installation package through an algorithm
yarn common instructions
-
##yarn init
: Initialize the project and generatepackage .jsonfile, the generation steps are roughly the same asnpm init
- ##yarn help
: Display command list
- yarn install
: Download all resources recorded in
package.json, which can be abbreviated asyarn - yarn add package name
:
Download thespecified package to the current directory - yarn remove package name
:
UninstallThe package specified in the current directory - yarn upgrade package name
:
UpdateSpecified package in the current directory, you can add @specified version number after the package name to specify the version to be updated
yarn command suffix
- ##--dev
-
: Specify the dependencies under
development environment( devDependencies), abbreviated as -D --peer -
: Specify
core dependencies( peerDependencies ) --optional -
: Specify
optional Dependencies(optionalDependencies) Conclusion
This article introduces npm
andyarn, as well as nrm## derived from npm #, cnpm, etc. bloggers have always used the combination of npm nrm switching source , because this not only ensures fast speed and convenient source switching, but also There is no need to download additional packages like
, yarn. npm and
have more content. This article only explains the most commonly used content. If you want to know more, you can go to the corresponding official website to view For more node-related knowledge, please visit: nodejs tutorial!
The above is the detailed content of Learn about two powerful Node package managers: npm and yarn. For more information, please follow other related articles on the PHP Chinese website!
JavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AMJavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.
JavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AMThe main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.
Understanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AMUnderstanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.
Python vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AMPython is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.
Python vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AMPython and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.
From C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AMThe shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.
JavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AMDifferent JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.
Beyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AMJavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool









