npmyarn
Posted JSong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了npmyarn相关的知识,希望对你有一定的参考价值。
1. yarn 安装
npm install -g yarn
yarn --version
npm install -g npm@latest --最新版本
yarn upgrade npm@latest
2. 国内镜像
yarn config set registry https://registry.npm.taobao.org -g
yarn config set sass_binary_site http://cdn.npm.taobao.org/dist/node-sass -g
npm config set registry https://registry.npm.taobao.org
淘宝npm镜像
搜索地址:http://npm.taobao.org
registry地址:http://registry.npm.taobao.org
cnpmjs镜像
搜索地址:http://cnpmjs.org
registry地址:http://r.cnpmjs.org
yarn无法搜索,npm换源后无法搜索,所以最终采用的方法是:
yarn更换源为国内镜像,npm保持搜索功能!
yarn config delete registry
npm config delete registry
.yarnrc配置
registry "https://registry.npm.taobao.org"
sass_binary_site "https://npm.taobao.org/mirrors/node-sass/"
phantomjs_cdnurl "http://cnpmjs.org/downloads"
electron_mirror "https://npm.taobao.org/mirrors/electron/"
sqlite3_binary_host_mirror "https://foxgis.oss-cn-shanghai.aliyuncs.com/"
profiler_binary_host_mirror "https://npm.taobao.org/mirrors/node-inspector/"
chromedriver_cdnurl "https://cdn.npm.taobao.org/dist/chromedriver"
.npmrc 配置
registry=https://registry.npm.taobao.org
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
phantomjs_cdnurl=http://cnpmjs.org/downloads
electron_mirror=https://npm.taobao.org/mirrors/electron/
sqlite3_binary_host_mirror=https://foxgis.oss-cn-shanghai.aliyuncs.com/
profiler_binary_host_mirror=https://npm.taobao.org/mirrors/node-inspector/
chromedriver_cdnurl=https://cdn.npm.taobao.org/dist/chromedriver
3. yarn和npm命令对比
npm | yarn | 注释 |
---|---|---|
npm init | yarn init | 初始化项目 |
npm install | yarn | 安装全部依赖 |
npm install react --save | yarn add react | 安装某个依赖,保存到 dependencies |
npm uninstall react --save | yarn remove react | 移除某个依赖 |
npm install react --save-dev | yarn add react --dev | 安装某依赖,保存到 devDependencies |
npm update [package] --save | yarn upgrade [package] | 更新生产环境某个依赖包 |
npm install axios --global | yarn global add axios | 全局安装某个依赖 |
npm install --save axios vue-axios | yarn add axios vue-axios | 同时下载多个依赖包 |
npm install [package]@[version] | yarn add [package]@[version] | 安装指定版本的包 |
npm rebuild | yarn install --force | 重新下载所有包 |
npm list -g --depth 0 | yarn global list --depth=0 | 全局安装过的包 |
4. npm 中 –save和–save-dev区别
npm5版本之前,必须要加上--save,才会将包记录到package.json
npm5版本之后开始,--save是默认值了,可以省略不写
dependencies【生产环境】
npm i jquery -S
npm i jquery --save
yarn add jquery -S //yarn 只有简写
devDependencies【开发环境】
npm i webpack --save-dev
npm i Webpack -D
yarn add webpack -D // yarn 只有简写
5. yarn error:computed integrity doesn\'t match our records
yarn cache clean
yarn --update-checksums
yarn
6. yarn offline 离线安装依赖
- 创建npm-packages-offline-cache文件夹,用于存放依赖包文件(*.tgz)
{any path}/npm-packages-offline-cache 查看、设置.yarnrc 文件
yarn config get yarn-offline-mirror yarn config set yarn-offline-mirror {any path}/npm-packages-offline-cache
创建项目文件package.json
{ "name": "yarn-offline", "version": "1.0.0", "main": "index.js", "license": "MIT", "dependencies": { "node-html-parser": "^3.3.5" } }
安装项目依赖(自动下载依赖文件至npm-packages-offline-cache)
yarn
新建项目,拷贝package.json,断网(模拟离线环境)
yarn install --offline
- 参考:https://www.jianshu.com/p/eb6...
7. 其他
#查看全局安装位置
yarn global bin
以上是关于npmyarn的主要内容,如果未能解决你的问题,请参考以下文章