EACCES:权限被拒绝,mkdir '/node_modules/node-sass/build' 在 ubuntu 上运行 npm install 时

Posted

技术标签:

【中文标题】EACCES:权限被拒绝,mkdir \'/node_modules/node-sass/build\' 在 ubuntu 上运行 npm install 时【英文标题】:EACCES: permission denied, mkdir '/node_modules/node-sass/build' while running npm install on ubuntuEACCES:权限被拒绝,mkdir '/node_modules/node-sass/build' 在 ubuntu 上运行 npm install 时 【发布时间】:2018-12-15 18:42:16 【问题描述】:

我有一个 Angular 4 应用程序,我想在 ubuntu 上运行它。我将所有项目文件、package.json 和 package-lock.json 复制到了 winscp 中的文件夹,但我没有复制 node_modules!现在当我运行 npm install 时,我得到如下错误:

> node-sass@4.7.2 install /home/ubuntu/cms/node_modules/@angular/cli/node_modules/node-sass
> node scripts/install.js

Unable to save binary /home/ubuntu/cms/node_modules/@angular/cli/node_modules/node-sass/vendor/linux-x64-57 :  Error: EACCES: permission denied, mkdir '/home/ubuntu/cms/node_modules/@angular/cli/node_modules/node-sass/vendor'
    at Object.fs.mkdirSync (fs.js:885:18)
    at sync (/home/ubuntu/cms/node_modules/mkdirp/index.js:71:13)
    at Function.sync (/home/ubuntu/cms/node_modules/mkdirp/index.js:77:24)
    at checkAndDownloadBinary (/home/ubuntu/cms/node_modules/@angular/cli/node_modules/node-sass/scripts/install.js:111:11)
    at Object.<anonymous> (/home/ubuntu/cms/node_modules/@angular/cli/node_modules/node-sass/scripts/install.js:154:1)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
  errno: -13,
  code: 'EACCES',
  syscall: 'mkdir',
  path: '/home/ubuntu/cms/node_modules/@angular/cli/node_modules/node-sass/vendor' 

> node-sass@4.9.1 install /home/ubuntu/cms/node_modules/node-sass
> node scripts/install.js

Unable to save binary /home/ubuntu/cms/node_modules/node-sass/vendor/linux-x64-57 :  Error: EACCES: permission denied, mkdir '/home/ubuntu/cms/node_modules/node-sass/vendor'
    at Object.fs.mkdirSync (fs.js:885:18)
    at sync (/home/ubuntu/cms/node_modules/mkdirp/index.js:71:13)
    at Function.sync (/home/ubuntu/cms/node_modules/mkdirp/index.js:77:24)
    at checkAndDownloadBinary (/home/ubuntu/cms/node_modules/node-sass/scripts/install.js:114:11)
    at Object.<anonymous> (/home/ubuntu/cms/node_modules/node-sass/scripts/install.js:157:1)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
  errno: -13,
  code: 'EACCES',
  syscall: 'mkdir',
  path: '/home/ubuntu/cms/node_modules/node-sass/vendor' 

> node-sass@4.7.2 postinstall /home/ubuntu/cms/node_modules/@angular/cli/node_modules/node-sass
> node scripts/build.js

Building: /usr/bin/node /home/ubuntu/cms/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 [ '/usr/bin/node',
gyp verb cli   '/home/ubuntu/cms/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 info using node-gyp@3.7.0
gyp info using node@8.9.4 | 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 platform; print(platform.python_version());"` returned: "2.7.12\n"
gyp verb get node dir no --target version specified, falling back to host node version: 8.9.4
gyp verb command install [ '8.9.4' ]
gyp verb install input version string "8.9.4"
gyp verb install installing version: 8.9.4
gyp verb install --ensure was passed, so won't reinstall if already installed
gyp verb install version is already installed, need to check "installVersion"
gyp verb got "installVersion" 9
gyp verb needs "installVersion" 9
gyp verb install version is good
gyp verb get node dir target node version installed: 8.9.4
gyp verb build dir attempting to create "build" dir: /home/ubuntu/cms/node_modules/@angular/cli/node_modules/node-sass/build
gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/home/ubuntu/cms/node_modules/@angular/cli/node_modules/node-sass/build'
gyp ERR! System Linux 4.4.0-124-generic
gyp ERR! command "/usr/bin/node" "/home/ubuntu/cms/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd /home/ubuntu/cms/node_modules/@angular/cli/node_modules/node-sass
gyp ERR! node -v v8.9.4
gyp ERR! node-gyp -v v3.7.0
gyp ERR! not ok
Build failed with error code: 1

> node-sass@4.9.1 postinstall /home/ubuntu/cms/node_modules/node-sass
> node scripts/build.js

Building: /usr/bin/node /home/ubuntu/cms/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 [ '/usr/bin/node',
gyp verb cli   '/home/ubuntu/cms/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 info using node-gyp@3.7.0
gyp info using node@8.9.4 | 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 platform; print(platform.python_version());"` returned: "2.7.12\n"
gyp verb get node dir no --target version specified, falling back to host node version: 8.9.4
gyp verb command install [ '8.9.4' ]
gyp verb install input version string "8.9.4"
gyp verb install installing version: 8.9.4
gyp verb install --ensure was passed, so won't reinstall if already installed
gyp verb install version is already installed, need to check "installVersion"
gyp verb got "installVersion" 9
gyp verb needs "installVersion" 9
gyp verb install version is good
gyp verb get node dir target node version installed: 8.9.4
gyp verb build dir attempting to create "build" dir: /home/ubuntu/cms/node_modules/node-sass/build
gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/home/ubuntu/cms/node_modules/node-sass/build'
gyp ERR! System Linux 4.4.0-124-generic
gyp ERR! command "/usr/bin/node" "/home/ubuntu/cms/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd /home/ubuntu/cms/node_modules/node-sass
gyp ERR! node -v v8.9.4
gyp ERR! node-gyp -v v3.7.0
gyp ERR! not ok
Build failed with error code: 1
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted "os":"darwin","arch":"any" (current: "os":"linux","arch":"x64")
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: node-sass@4.7.2 (node_modules/@angular/cli/node_modules/node-sass):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: node-sass@4.7.2 postinstall: `node scripts/build.js`
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Exit status 1

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-sass@4.9.1 postinstall: `node scripts/build.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-sass@4.9.1 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!     /home/ubuntu/.npm/_logs/2018-07-07T10_49_32_298Z-debug.log

我按照我之前所说的步骤来运行我的项目,但我不知道现在发生了什么? 我也做了 rm -rf node_modules, rm package-lock.json, npm cache clean --force 并再次使用了 npm install 但它仍然无法正常工作!

【问题讨论】:

你试过用sudo运行这些命令吗? @R.Richards 是的,我做到了!但仍然是同样的错误 你有没有想过这个问题?我在运行 ubuntu 18.04 时遇到完全相同的错误 【参考方案1】:

安装时添加 --unsafe-perm 标志

适用于 linux/mac

sudo npm install --unsafe-perm -g node-sass

对于 windows 环境,点击相同的命令删除 sudo

【讨论】:

不需要sudo 和全局标志-g。这很好 -> npm install --unsafe-perm node-sass【参考方案2】:

在 Linux/mac 上

npm install --unsafe-perm node-sass

只需添加 unsafe-perm 即可。使用 sudo 会给node modules 提供不必要的权限,并弄乱文件系统。

【讨论】:

感谢@A1rPun 提出这种方法。

以上是关于EACCES:权限被拒绝,mkdir '/node_modules/node-sass/build' 在 ubuntu 上运行 npm install 时的主要内容,如果未能解决你的问题,请参考以下文章

AWS:EACCES:权限被拒绝,mkdir '/home/react-app/node_modules/node-sass/vendor'

EACCES:权限被拒绝,mkdir '/node_modules/node-sass/build' 在 ubuntu 上运行 npm install 时

错误:EACCES:权限被拒绝,mkdir '/.npm'

EACCES:权限被拒绝 mkdir ...尝试在节点项目中使用 docker 卷时

将 React 添加到工作区失败,EACCES:权限被拒绝,mkdir '/home/mint/.cache/Cypress'

Errno::EACCES(权限被拒绝@dir_s_mkdir