
包管理
title: 包管理工具设置镜像
npm
npm设置淘宝镜像
npm config set registry https://registry.npm.taobao.org
npm config set strict-ssl false
换回原本镜像
npm config set registry https://registry.npmjs.org/
查看当前镜像
npm get registry
yarn
安装yarn
npm install yarn -g
yarn设置淘宝镜像
yarn config set registry http://registry.npm.taobao.org/
换回原本镜像
yarn config set registry http://registry.npmjs.org/
常用镜像
npm install phantomjs --phantomjs_cdnurl=http://npm.taobao.org/mirrors/phantomjs
npm install chromedriver --chromedriver_cdnurl=http://npm.taobao.org/mirrors/chromedriver
npm install operadriver --operadriver_cdnurl=http://npm.taobao.org/mirrors/operadriver
npm config set disturl https://npm.taobao.org/dist
npm config set electron_mirror https://npm.taobao.org/mirrors/electron/
npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/
npm config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs/
yarn config set disturl https://npm.taobao.org/dist -g
yarn config set electron_mirror https://npm.taobao.org/mirrors/electron/ -g
yarn config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/ -g
yarn config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs/ -g
yarn config set chromedriver_cdnurl https://cdn.npm.taobao.org/dist/chromedriver -g
yarn config set operadriver_cdnurl https://cdn.npm.taobao.org/dist/operadriver -g
yarn config set fse_binary_host_mirror https://npm.taobao.org/mirrors/fsevents -g
全局
yarn global bin
报错解决
无法加载文件
无法加载文件 C:\Users\Administrator\AppData\Roaming
pm\yarn.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参
阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
解决方法:
执行命令 set-ExecutionPolicy RemoteSigned
Yarn
初始化项目
yarn init 与 npm init 一样通过交互式会话创建一个 package.json
yarn init # yarn
npm init # npm
# 跳过会话,直接通过默认值生成 package.json
yarn init --yes # 简写 -y
npm init -y
安装包
yarn add 包名
npm install 包名
yarn add 包名 -D 会记录在 package.json 的 devDependencies开发环境中
npm i 包名 -D
yarn global add 包名 全局安装
npm i 包名 -g
更新一个依赖
yarn upgrade # 升级所有依赖项,不记录在 package.json 中
npm update # npm 可以通过 ‘--save|-D’ 指定升级哪类依赖
yarn upgrade 包名 # 升级指定包
npm update 包名
yarn upgrade --latest 忽略版本规则,升级到最新版本,并且更新 package.json
移除一个依赖
yarn remove 包名
npm uninstall 包名
安装 package.json 中的所有文件
yarn 在 node_modules 目录安装 package.json 中列出的所又依赖
npm i
yarn install 安装时,如果 node_modules 中有相应的包则不会重新下载 --force 可以强制重新下载安装
yarn install --force 强制下载安装
npm i --force
运行脚本
yarn run 用来执行在 package.json 中 scripts 属性下定义的脚本
// package.json
{
"scripts": {
"dev": "node app.js",
"start": "node app.js"
}
}
yarn run dev # yarn 执行 dev 对应的脚本 node app.js
npm run # npm
yarn start # yarn
npm start # npm
显示某个包信息
yarn info 包名 # yarn
npm info 包名 # npm
yarn info 包名 --json # 输出 json 格式
npm info 包名 --json # npm
yarn info 包名 readme # 输出 README 部分
npm info 包名 readme
列出项目的所有依赖
yarn list # 列出当前项目的依赖
npm list # npm
yarn list --depth=0 # 限制依赖的深度
sudo yarn global list # 列出全局安装的模块
缓存
sudo yarn cache list # 列出已缓存的每个包
sudo yarn cache dir # 返回 全局缓存位置
sudo yarn cache clean # 清除缓存
pnmp
安装
npm install pnpm -g
设置淘宝镜像
pnpm config set registry https://registry.npm.taobao.org
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 程序员HoneyZ
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果