当前位置:网站首页>About npm/cnpm/npx/pnpm and yarn

About npm/cnpm/npx/pnpm and yarn

2022-08-10 19:02:00 youhebuke225

package.json

dependencies

  • 安装在dependencies中的包,Generally, it is the package that the online project depends on
  • Generally install the package inside,项目就可以正常运行了
  • 通过NODE_ENV=production npm i来进行安装

devDependencies

  • Packages required for the development environment,Generally some kits,We are all installed in it,如eslint等
  • Some package escape toolkits,Usually put here,如webpackloader

包的版本

The packages we install are generally as follows

{
    
	...
    "core-js": "3.20.3",
    "css-loader": "^3.5.3",
    "es6-promise": "^4.1.0",
    "fast-sass-loader": "^2.0.1",
    ...
}

命名格式

  • "core-js": "3.20.3",After him is the version number
    • 3是主版本号,代表的是不兼容的 API 修改
    • 20是次版本号,代表的是向下兼容的功能性新增
    • 3是修订号,代表的是向下兼容的问题修正
  • 此外,We will also find that in front of the version number,还有一些特殊的符号
    • ~Indicates when we are updating the package,Only update patch versions
      • 如果写入的是 〜0.13.0,则Only update patch versions:即 0.13.1 可以,但 0.14.0 不可以.
    • ^Indicates when we are updating the package,Update the patch version and this version
      • 如果写入的是 ^0.13.0,则要更新补丁版本和次版本:即 0.13.1、0.14.0、依此类推.
    • 如果没有前缀,always keep this version
      • 如果写入的是 0.13.0,则始终使用确切的版本.

package-lock.json

  • 从npm5版本之后,就推出了package-lock.json
  • package-lock.json的优先级会高于package.json,package-lock.jsonPins the package version of the current project
  • 想要更新package-lock.json,We just have to use commandsnpm update
    • But in this case, all the dependent packages will be upgraded
    • Still recommend if you need to upgrade,Just upgrade a package alone

npm

npm 是nodejsthe standard package manager,通常,The packages we installed are all therepackage.json中体现

  • 因为npmLet us compare the commonly used package management tools,So we will not introduce it in detail here
  • 常用命令 点击

yarn

  • yarnThe current rationpm会快一些,是因为yarnThe current installation is a side-by-side installation,而npmis a serial install
  • 在缓存机制上 ,两者都差不多,都会读取本地缓存
  • But in the display and design of the installation packageyarn会更友好一点
  • 常用命令 点击

cnpm

  • cnpm的镜像仓库在国内,也就是淘宝,我们可以全局安装cnpm
npm install -g cnpm --registry=https://registry.npm.taobao.org
  • 当然我们也可以把npmThe mirror library is set to Taobao source
npm config set registry http://registry.npm.taobao.org
  • 需要注意的是cnpm没有package-lock.json,即我们使用cnpm的时候,It doesn't happen automaticallypackage-lock.json,Even if the project has itpackage-lock.jsonHe also doesn't care,只读取package.json
  • This can lead to some unexpected thingsbug,并且有时候cnpmnpmThe downloaded package is incompatible
  • 所以,能用npmJust try not to use itcnpm

pnpm

简介

pnpm最大的特点就是

  1. 包安装速度极快
  2. 磁盘空间利用效率高
    1. 硬链接:硬连接指通过索引节点来进行连接.在Linux的文件系统中,保存在磁盘分区中的文件不管是什么类型都给它分配一个编号,称为索引节点号(Inode Index).在Linux中,多个文件名指向同一索引节点是存在的.一般这种连接就是硬连接.硬连接的作用是允许一个文件拥有多个有效路径名,这样用户就可以建立硬连接到重要文件,以防止“误删”的功能.其原因如上所述,因为对应该目录的索引节点有一个以上的连接.只删除一个连接并不影响索引节点本身和其它的连接,只有当最后一个连接被删除后,文件的数据块及目录的连接才会被释放.也就是说,文件真正删除的条件是与之相关的所有硬连接文件均被删除.
    2. 软连接:另外一种连接称之为符号连接,也叫软连接.软链接文件有类似于Windows的快捷方式.它实际上是一个特殊的文件.在符号连接中,文件实际上是一个文本文件,其中包含的有另一文件的位置信息.

也就是说

  1. 不会重复安装同一个包.使用npm/yarn 的时候,如果100个包依赖lodash ,那么就可能安装了100次lodash ,磁盘中就有100个地方写入了这部分代码.但是pnpm会只在一个地方写入这部分代码,It will be used directly laterhard link
  2. 即使一个包的不同版本,pnpm 也会极大程度地复用之前版本的代码.举个例子,比如 lodash 有 100 个文件,更新版本之后多了一个文件,那么磁盘当中并不会重新写入 101 个文件,而是保留原来的 100 个文件的 hardlink,仅仅写入那一个新增的文件.

安装

官网

npm i pnpm -g

文章参考
Article reference two

原网站

版权声明
本文为[youhebuke225]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208101837304382.html