一般npm 编译分两步:
npm install: 根据项目的package.json下载 packages
npm run : 执行package.json 中scripts提前定义好的命令进行编译
有时候执行 npm install 权限不足的错误
gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/node_modules/node-sass/.node-gyp'
gyp ERR! System Linux 3.10.0-693.21.1.el7.x86_64
gyp ERR! node -v v14.17.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
为什么我用root命令执行还会出现权限不足呢?
原来 npm install 会自动切换到nobody用会执行,使用 --unsafe-perm,就不会切换到nobody用户权限了,官方介绍如下:
--unsafe-perm
If npm was invoked with root privileges, then it will change the uid to the user account or uid specified by the user config, which defaults to nobody. Set the unsafe-perm flag to run scripts with root privileges.
如果出现版本冲突,可以试试下面参数
--no-optional 将阻止安装可选的依赖项。避免导致下版本冲突。
官网介绍 如下:
--no-optional
argument will prevent optional dependencies from being installed.
另外在介绍一个命令 npm ci 它旨在用于自动化环境,例如测试平台、持续集成和部署,确保对依赖项进行全新安装
- 必须有一个 package-lock.json 或 npm-shrinkwrap.json,速度更快
- npm ci 开始安装之前自动删除node_modules文件夹,速度更快
- 永远不会写入
package.json
或任何包锁