Get into the habit of writing together! This is the first day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .
Hello everyone, I am Mao Xiaobai. This article continues to output the front-end confusion knowledge point series
第三问
. Committed to consolidating the foundation and eliminating vague knowledge points.
Previous [Confused Knowledge] Portal:
- One question: module.exports, exports, export are all exports, what is the difference?
- Second question: It's 2022, do you know about mandatory caching and negotiated caching?
The focus of this article is as follows:
- A diagram to understand the development history of package managers .
npm
What is it, and how did it go from being criticized by everyone to being the master?npx
what is it and what problem does it solvecnpm
what is it and what problem does it solveyarn
what is it and its advantagespnpm
what is it and its advantages
In the years of front-end development, I found that package management tools are also quite curly. When I first learned the front-end, it was not easy for me as a rookie to npm
learn , and something came up npm install
later . For me, who can only write in 15 years , these are hardly used, so I don't care at all. With the rise of front-end frameworks , , , , and other technologies, package managers have become an essential skill for every front-end programmer.cnpm
yarn
html+jquery
node
vue
react
angular
webpack
So why do these companies or teams spend so much effort developing yarn
, cnpm
, and pnpm
writing new package managers? In fact, in the process of development, everyone will encounter problems such as slow package download, various bugs caused by different dependencies, etc. In fact, these tools are to solve these problems.
Before we differentiate between these tools/downloaders, take a look at a timeline graph to see when each tool was born.
Time course diagram
It can be clearly seen in the figure:
npm
The first edition was born in 2010- 时隔4年的2014年
cnpm
诞生 - 2015年
npm
发布v3版,改掉了v2版本嵌套目录问题,将依赖扁平化 - 其实2016年
pnpm
就已经诞生,只是功能还不齐全,不被人熟知 - 2016年
npm@4
和yarn
同年同月发布,此时的yarn
轰动一时,赢来大众喜爱,yarn
各指标远超npm@4
- 隔半年的2017年5月
npm@5
版本发布,各项功能提升。像是参考了一波yarn
,差距缩小。 - 2017年7月
[email protected]
发布,npx
命令诞生 - 2018年5月
npm@6
发布,性能提升、npm init <pkg>
命令
npm是什么,如何从众人诟病到翻身做主人
这个不用过多废话,大家非常熟悉。总的来说就是一个开源、免费的包管理器。便于我们下载和分享资源。
cnpm、yarn、pnpm
等工具都是基于npm
包管理器的一些变种。解决了早期npm
的一些缺点,例如下载速度慢,不能离线下载等。
npx
是[email protected]版本的一个命令。下一节会单独说到
主要的npm
版本更新日程:
- [email protected] 首次发布--2010年
- [email protected] node_modules目录结构扁平化 --2015年06月
- [email protected] package-lock.json前身npm-shrinkwrap.json用于依赖锁定--2016年10月
- [email protected] package-lock.json默认生成,并兼容npm-shrinkwrap.json,重构npm-cache,大大提升下载速度 --2017年05月
- [email protected] npx命令发布 --2017年07月
- [email protected] 增加npm init --2018年05月
npx是什么,它解决了什么问题
npx
是npm5.2版本新增的一个命令,如果npm
版本没到v5.2,请运行npm install -g npx
可以运行本地的模块
例如在vue项目中,想运行本地的 vue-cli-service serve
如果直接在命令行运行会报错:找不到命令
所以我们通常这样:
package.json
中:
//....其它配置
"scripts": {
"dev": "vue-cli-service serve",
},
//....其它配置
复制代码
然后:
npm run dev
复制代码
然而现在,通过npx
可以这样:
npx cli-service serve
复制代码
运行成功:
npx方便使用一次就丢弃的情况,不会全局安装
例如create-react-app
,以往我们需要安装全局的包。但是使用一次后面几乎就不怎么使用了,非常浪费磁盘空间。现在我们可以用npx create-react-app myapp
,用完就删,真香!
如果第一次使用这个命令,
npx
会下载create-react-app
放在临时文件中,过一段时间会自动清除,注意不会立即清除,我测试发现第二次运行npx create-react-app myapp
不会重新下载,它会从缓存中获取。
还有其它好用的功能
npx
可以下载指定的版本,可以指定node
版本运行等请参考阮一峰老师的npx
介绍,全面易懂
npm init和npx相似
在npm@6
版本中,增加了一个新命令npm init <pkg>
这两个命令是等价的:
npm init react-app myapp
复制代码
npx create-react-app myapp
复制代码
npm init <pkg>
对与create
开头的脚手架制定了一个特殊命令,例如create-react-app、create-esm
。npm init
下载时会默认对安装的pkg
包添加create
前缀,同时像npx
一样不会全局安装,只是运行一次,后面会删除。
但npm init <pkg>
不能完全取代npx
,例如运行npm init http-server
本意是想一次性启动一个本地服务 结果报错:
途中可以看到http-server
被变成了create-http-server
,所以就找不到该模块,推荐用npx
就好,至少使用起来更可控。
yarn是什么,它解决了什么问题
yarn
也是一个包管理器,它和npm
其实没有本质区别,都是管理和安装包的。只是它解决了早期npm
的一些问题比如:不支持离线模式、树形结构的依赖、依赖安装不确定性等。为什么说是早期?因为npm
新版本基本上已经解决了自身的老毛病,两者没有想象中那么大的区别。
从最新版的
npm
和yarn
来看,他们的安装速度和使用体验并没有多大的差距,yarn
稍胜一筹。
安装:
npm install -g yarn
复制代码
安装包:
yarn add [package]
复制代码
删除包:
yarn remove [package]
复制代码
yarn相当于npm
优势(早期):
这里列出一些早期的yarn
相对于npm
比较大的优势:
- 支持离线安装(
npm@5
已支持) - 依赖扁平化结构(
npm@3
已支持) - 依赖安装确定性
yarn.lock
(npm@5增加了package-lock.json
) - 安装速度快并行下载
- 安装失败自动重试
- 等等...
pnpm是什么,它解决了什么问题
pnpm
也是一个包管理器,它巧妙的使用了类似于linux
的软连接方式,达到一个模块文件多处服用,解决了yarn、npm
在多个项目安装同一个依赖时会下载重复文件的问题,避免磁盘的浪费,同时大大提升下载速度。
下面是pnpm
的一些特点:
pnpm
运行起来非常的快,超过了npm
和yarn
。pnpm
采用了一种巧妙的方法,利用硬链接和符号链接来避免复制所有本地缓存源文件。也就是说多个项目相同的依赖只会在某处安装一次,连接过来直接使用,节省了安装时间和瓷盘空间。pnpm
继承了yarn
和新版npm
的所有优点,包括离线模式和确定性安装。- 但是链接在一些场景下会存在兼容的问题,例如
Electron
应用无法使用pnpm
、部署在lambda
上的应用无法使用pnpm
总结
npm
是一个包管理器,方便开发者分享和下载开源包。经历了许多重大版本的更新,各方面已经和yar
n在同一水平。npx
是[email protected]
的产物,方便运行本地命令cnpm
是方便中国开发者下载依赖包而诞生的下载器。yarn
解决了npm@5
之前的一些让人诟病的问题,同时拥有一些其它的优点。例如离线安装、失败自动重试安装和并行下载等。pnpm
通过连接的方式,让多个项目的依赖公用同一个包,大大节省了磁盘空间,比yarn
和npm
下载速度快得多,但是也有连接带来的各种兼容问题。
从个人角度来说,使用
npm
、yarn
、pnpm
都是可以的,但是最好是团队都使用同一个管理器。
以上。
肯请各位大佬,不要忘了给我点赞
、评论
、收藏
。