1.node 安装
npm是node安装包中默认自带的,推荐使用nvm等node版本管理工具安装,避免可能的权限问题
npm 版本验证
1  | $ node -v  | 
npm 更新
1  | [sudo] npm install npm -g  | 
npm latest安装
1  | npm install npm@latest -g  | 
npm next安装
1  | npm install npm@next -g  | 
npm-debug.log定位失败原因
npm install or npm publish 失败时候,通常.npm文件夹下会产生日志npm-debug.log
cli构建或者npm Enterprise用户,不适用此方法,可参考
确定日志文件所在路径
1  | $ npm config get cache  | 
开启日志
1  | # install  | 
2.yarn 安装
安装
全局安装
1  | npm install -g yarn  | 
Home Brew
1  | brew install yarn  | 
script脚本安装
1  | curl -o- -L https://yarnpkg.com/install.sh | bash  | 
tar包安装
1  | cd /opt  | 
yarn 版本验证
1  | $ yarn --version  | 
yarn 版本升级
1  | brew upgrade yarn  | 
安装失败 yarn-error.log
查看路径
1  | $ npm config get cache  | 
查看日志yarn-error.log
1  | /Users/hu/.npm/yarn-error.log  | 
3.npm cli 与 yarn cli 常用命令类比
| 内容 | npm | yarn | 
|---|---|---|
| 初始化工程 | npm init [-y] | yarn init | 
| 添加依赖 | npm install [package] | yarn add [package] | 
| 添加特定版本依赖 | npm install [package]@[version] | yarn add [package]@[version] | 
| 添加特定tag依赖 | npm install [package]@[tag] | yarn add [package]@[tag] | 
| 开发依赖 | npm install [pageckage] - -save-dev(-D) | yarn add [package] - -dev | 
| 运行时依赖 | npm install [package] - -save(-P) | yarn add [package] | 
| optionalDependencies | npm install [package] - -save-optional | yarn add [package] –optional | 
| 更新依赖 | npm upgrade [pageckage] | yarn upgrade [package] | 
| 更新特定版本依赖 | npm upgrade [pageckage]@[version] | yarn upgrade [package]@[version] | 
| 更新特定tag依赖 | npm upgrade [pageckage]@[tag] | yarn upgrade [package]@[tag] | 
| 删除依赖 | npm remove [pageckage] | yarn remove [package] | 
| 整体安装 | npm install | yarn | 
4. npm cli 与 yarn cli命令列表区别
| npm | yarn | 描述 | 
|---|---|---|
| npm install [package] | yarn add [package] | 添加单个依赖 | 
| npm audit | yarn audit | 安全性校验 | 
| / | yarn autoclean | 自我移除不需要的依赖 | 
| npm bin | yarn bin | |
| npm build | / | 包构建 | 
| npm cache | yarn cache | 本地缓存相关管理 | 
| npm doctor 与 npm outdated | yarn check | 两者不同,npm是判断环境和是否过期,yarn 是check依赖是否正确 | 
| npm config | yarn config | 配置信息 | 
| / | yarn create | yarn create 利用create-*模板创建项目 | 
| npm install -g | yarn global | 全局安装 | 
| npm help | yarn help | 帮助信息 | 
| / | yarn import | 从package-lock.json 得到yarn.lock | 
| / | yarn info [package] | 展示package 信息 | 
| npm init | yarn init | 创建package.json | 
| npm install | yarn install | 根据package.json配置,添加整体依赖 | 
| npm ci | / | 根据package-lock.json配置,添加依赖,没有package-lock.json,报错退出 | 
| / | yarn licenses | 列出所有安装包的licenses | 
| npm link | yarn link | 创建符号链接 | 
| npm ls | yarn list | 列表已经安装的依赖 | 
| / | yarn login | 保存npm 注册填写username,email | 
| npm logout | yarn logout | 清空保存的username,email | 
| npm outdated | yarn outdated | check 版本是否过期 | 
| npm owner | yarn owner | 包管理者相关概念 | 
| npm pack | yarn pack | 创建压缩包 | 
| npm publish | yarn publish | 发布包 | 
| npm uninstall | yarn remove | 删除包依赖 | 
| npm run | yarn run | 运行scripts脚本命令 | 
| / | yarn self-update | yarn更新到最新 | 
| npm dist-tag | yarn tag | 依赖tag管理 | 
| npm team | yarn team | team信息维护 | 
| npm test | yarn test | 包测试 | 
| / | yarn unlink | 删除符号链接 | 
| npm upgrade | yarn upgrade | 更新依赖 | 
| npm version | yarn version | 版本验证 | 
| / | yarn versions | 展示安装的node,yarn,依赖版本信息 | 
5.npm 安装常见18错误
1.Broken npm installation
解决方法如下:
- Mac or Linux,重新安装
 - Windows,从官网下载node安装包,重新安装
 
2.Random errors
解决方法如下:
- 运行如下命令 
1
2# 清楚缓存
npm cache clean - 查看具体的错误,分析原因 
1
npm install xxx -verbose
 
3.No compatible version found
npm版本过低,需要升级
解决方法如下:
1  | npm install -g npm@latest  | 
4.Permissions errors
权限问题,安装路径拥有的权限有关,推荐使用nvm等包管理工具安装
5.Error: ENOENT,…
Error: ENOENT, stat ‘C:\Users<user>\AppData\Roaming\npm’ on Windows 7
需要确保路径存在且可写入
1  | C:\Users\<user>\AppData\Roaming\npm  | 
6.No space
npm ERR! Error: ENOSPC, write。原因在于没有空间,或者权限不足,
解决方法如下:
- 释放磁盘空间
 - 修改路径 
1
npm config set tmp /path/to/big/drive/tmp
 - 最终跟换有空间的服务器安装
 
7.No git
npm ERR! not found: git ENOGIT 。
解决方法如下:
8.Running a Vagrant box…
9.npm only uses git…
解决方法如下:
1  | git config --global url."https://github.com/".insteadOf git@github.com:  | 
10.SSL error
解决方法如下:
- 禁用 strict-ssl 
1
2# or
npm config set strict-ssl false 
- 禁用 strict-ssl 
 - 查看node版本,过低,先升级node
 
- 更新npm 
1
2npm install npm -g --ca=""
npm config set ca "" 
- 更新npm 
 
12.Not found / Server error
解决方法如下:
- 确保安装包存在
 - 检查网络
 
13.Invalid JSON
解放方法如下:
- 清除缓存 
1
npm cache clean
 - 检查proxy配置
 
14.Many ENOENT / ENOTEMPTY errors in output
解决方法如下:
1  | # 升级  | 
15.cb() never called!
1  | # 升级  | 
16.npm login errors
解决步骤如下:
- 登录npmjs.com
 - 修改密码
 - 清除相关文件
1
sed -ie '/registry.npmjs.org/d' ~/.npmrc
 - 重新登录
 
17.npm hangs on Windows…
解决步骤如下:
- 查看tmp文件夹
1
npm config ls -l
 - 删除多余的一个
 
18.npm not running the latest version…
解决方法,请移步
npm ci
参考
Npm Doc
Yarn Doc
npm cli
          赏
          
            
              
              使用支付宝打赏
            
            
              
              使用微信打赏
            
          
        
        若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏