Home >Web Front-end >JS Tutorial >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 yarn
thesethird-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!
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
NodeJS
Package managementis reflected in that it is a warehouse of NodeJS, which stores and manages various software packages of
NodeJS
Distribution toolin our configurationis reflected in using the npm command to download the package in the
npm
warehouse
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
npm 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-g
represents global installation, thesenpm
instructions 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
we generally call npm just refers to the command module of npm (the package named npm)But in fact, the word
npmrefers to the
npm command Module
also refers to thenpm
NodeJS
open source warehouse itself, so we have it in
npm
(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
: Generate package.json
Download all resources recorded in
package.json
Download specified package to the current directory
Uninstallcurrent 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 updated
npm 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 checked
npm info package name
:Getdetailed information of the package in the current directory
npm list:
View all packages installed in the current directory and their dependencies and display the version number (list can Abbreviated as ls
)
:View
specified package installed in the current directory The version number (list can be abbreviated as ls)
i, for example:
npm install axios can be abbreviated to
npm i axios
un
npm i md5@1 Download version 1 of md5,
npm i md5@latest means download the latest version of md5
Command suffix
: Specify Global environment
The default command is to operate in the current directory, adding#--save-g
is specified in the
global environmentOperation, install the latest version of npm globally as mentioned above:
npm i npm@latest -g, so that npm## can be used in anydirectory
: Specify the dependencies under production environment
( Recorded in dependencies
) After the npm5
version, 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
: Specify the dependencies under development environment (recorded in
devDependencies)
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
: the same as --save--save -optional
: Specify optional dependencies (recorded in
optionalDependencies)
--no-save
For 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
:
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
将会安装md5
2.x.x
的最新版本(2大版本下的最新版本),并不一定是2.1.0,还可能是2.3.0
包版本以~
开头时,会锁定到第二个大版本
// package.json "dependencies": { "md5": "~2.1.0" },
通过
npm i
将会安装md5
2.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是Facebook发布的一款依赖管理工具,它比npm
更快、更高效
安装:
npm install -g yarn
更新yarn:
yarn set version latest yarn set version from sources
优点
速度超快yarn
缓存了每个下载过的包,所以再次使用时无需重复下载。 同时利用并行下载以最大化资源利用率,因此安装速度更快
Super safe
Before executing the code, yarn
will verify the integrity of each installation package through an algorithm
yarn common instructions
##yarn init: Initialize the project and generate
package .json file, the generation steps are roughly the same as
npm init
: Display command list
: Download all resources recorded in package.json
, which can be abbreviated as yarn
: Download the
specified package to the current directory
: Uninstall
The package specified in the current directory
:Update
Specified 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
development environment
( devDependencies), abbreviated as -D
core dependencies
( peerDependencies )
optional Dependencies
(optionalDependencies)
yarn, 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!