TL;DR
确认node是否有修改源代码文件夹的权限;包括确认文件owner和文件修改权限。
过程 & 思路
日前在服务器上安装
node-sass
的过程中遇到了以下错误:
[root@VM_0_7_centos JerryChan31.github.io]# npm i
> [email protected] install /root/JerryChan31.github.io/node_modules/node-sass
> node scripts/install.js
Unable to save binary /root/JerryChan31.github.io/node_modules/node-sass/vendor/linux-x64-72 : Error: EACCES: permission denied, mkdir '/root/JerryChan31.github.io/node_modules/node-sass/vendor'
at Object.mkdirSync (fs.js:840:3)
at sync (/root/JerryChan31.github.io/node_modules/mkdirp/index.js:72:13)
at Function.sync (/root/JerryChan31.github.io/node_modules/mkdirp/index.js:78:24)
at checkAndDownloadBinary (/root/JerryChan31.github.io/node_modules/node-sass/scripts/install.js:114:11)
at Object.<anonymous> (/root/JerryChan31.github.io/node_modules/node-sass/scripts/install.js:157:1)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) {
errno: -13,
syscall: 'mkdir',
code: 'EACCES',
path: '/root/JerryChan31.github.io/node_modules/node-sass/vendor'
> [email protected] postinstall /root/JerryChan31.github.io/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"
Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!
The project needs your help! Please consider supporting of core-js on Open Collective or Patreon:
> https://opencollective.com/core-js
> https://www.patreon.com/zloirock
Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)
> [email protected] postinstall /root/JerryChan31.github.io/node_modules/ejs
> node ./postinstall.js
Thank you for installing EJS: built with the Jake JavaScript build tool (https://jakejs.com/)
> [email protected] postinstall /root/JerryChan31.github.io/node_modules/node-sass
> node scripts/build.js
Building: /usr/local/bin/node/bin/node /root/JerryChan31.github.io/node_modules/node-gyp/bin/node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=
gyp info it worked if it ends with ok
gyp verb cli [
gyp verb cli '/usr/local/bin/node/bin/node',
gyp verb cli '/root/JerryChan31.github.io/node_modules/node-gyp/bin/node-gyp.js',
gyp verb cli 'rebuild',
gyp verb cli '--verbose',
gyp verb cli '--libsass_ext=',
gyp verb cli '--libsass_cflags=',
gyp verb cli '--libsass_ldflags=',
gyp verb cli '--libsass_library='
gyp verb cli ]
gyp info using [email protected]
gyp info using [email protected] | linux | x64
gyp verb command rebuild []
gyp verb command clean []
gyp verb clean removing "build" directory
gyp verb command configure []
gyp verb check python checking for Python executable "python2" in the PATH
gyp verb `which` succeeded python2 /usr/bin/python2
gyp verb check python version `/usr/bin/python2 -c "import sys; print "2.7.5
gyp verb check python version .%s.%s" % sys.version_info[:3];"` returned: %j
gyp verb get node dir no --target version specified, falling back to host node version: 12.16.1
gyp verb command install [ '12.16.1' ]
gyp verb install input version string "12.16.1"
gyp verb install installing version: 12.16.1
gyp verb install --ensure was passed, so won't reinstall if already installed
gyp verb install version not already installed, continuing with install 12.16.1
gyp verb ensuring nodedir is created /root/.node-gyp/12.16.1
gyp WARN EACCES user "root" does not have permission to access the dev dir "/root/.node-gyp/12.16.1"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/root/JerryChan31.github.io/node_modules/node-sass/.node-gyp"
gyp verb tmpdir == cwd automatically will remove dev files after to save disk space
gyp verb command install [ '--node_gyp_internal_noretry', '12.16.1' ]
gyp verb install input version string "12.16.1"
gyp verb install installing version: 12.16.1
gyp verb install --ensure was passed, so won't reinstall if already installed
gyp verb install version not already installed, continuing with install 12.16.1
gyp verb ensuring nodedir is created /root/JerryChan31.github.io/node_modules/node-sass/.node-gyp/12.16.1
gyp WARN install got an error, rolling back install
gyp verb command remove [ '12.16.1' ]
gyp verb remove using node-gyp dir: /root/JerryChan31.github.io/node_modules/node-sass/.node-gyp
gyp verb remove removing target version: 12.16.1
gyp verb remove removing development files for version: 12.16.1
gyp WARN install got an error, rolling back install
gyp verb command remove [ '12.16.1' ]
gyp verb remove using node-gyp dir: /root/JerryChan31.github.io/node_modules/node-sass/.node-gyp
gyp verb remove removing target version: 12.16.1
gyp verb remove removing development files for version: 12.16.1
gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/root/JerryChan31.github.io/node_modules/node-sass/.node-gyp'
gyp ERR! System Linux 3.10.0-1062.9.1.el7.x86_64
gyp ERR! command "/usr/local/bin/node/bin/node" "/root/JerryChan31.github.io/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd /root/JerryChan31.github.io/node_modules/node-sass
gyp ERR! node -v v12.16.1
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
Build failed with error code: 1
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] postinstall: `node scripts/build.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2021-04-27T09_42_26_856Z-debug.log
网上一查,查到 node-sass的Github issue ,里面只提到了没有权限,但是没有提到如何修复权限问题。
中文网络下的搜索结果大部分也只是让人使用
npm i --unsafe-perm node-sass
这样的命令来绕过问题。
下面是我解决这个问题的流程。
仔细看下这里的报错堆栈:
gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/root/JerryChan31.github.io/node_modules/node-sass/.node-gyp'
gyp ERR! System Linux 3.10.0-1062.9.1.el7.x86_64