Npm 错误“错误:EACCES:权限被拒绝,mkdir '../node-sass/build'" [英] Npm error "Error: EACCES: permission denied, mkdir '../node-sass/build'"
问题描述
无法通过 npm sudo npm i
安装 node-sass 包,我一直在尝试通过 sudo npm i -g node-sass --unsafe-perm=true 解决--allow-root
但它不起作用.chmod 777 要么.我认为有一些具有 npm 权限的东西,但我不知道日志:
gyp 动词构建目录试图创建构建"目录:/home/michael/Desktop/react/node_modules/node-sass/build吉普错误!配置错误吉普错误!堆栈错误:EACCES:权限被拒绝,mkdir '/home/michael/Desktop/react/node_modules/node-sass/build'吉普错误!系统 Linux 4.15.0-47-generic吉普错误!命令 "/usr/bin/node" "/home/michael/Desktop/react/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=""--libsass_ldflags=""--libsass_library="吉普错误!cwd/home/michael/Desktop/react/node_modules/node-sass吉普错误!节点 -v v11.14.0吉普错误!节点-gyp -v v3.8.0吉普错误!不好构建失败,错误代码:1npm WARN 可选 SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.7 (node_modules/fsevents):npm WARN notsup 跳过可选依赖项:fsevents@1.2.7 不受支持的平台:想要 {"os":"darwin","arch":"any"}(当前:{"os":"linux","arch":"x64"})npm 错误!代码生命周期npm 错误!错误号 1npm 错误!node-sass@4.11.0 安装后:`节点脚本/build.js`npm 错误!退出状态 1npm 错误!npm 错误!在 node-sass@4.11.0 安装后脚本失败.npm 错误!这可能不是 npm 的问题.上面可能有额外的日志输出.npm 错误!可以在以下位置找到此运行的完整日志:npm 错误!/home/michael/.npm/_logs/2019-04-13T17_21_20_424Z-debug.log
作为一般建议,永远不要使用 sudo 运行 npm install 因为它会创建由 root 而不是您的用户拥有的本地文件,我认为您应该尝试以下操作:
- 首先更改文件夹的所有者:
chown -R yourusername:yourusername/home/michael/Desktop/react/
,或者如果您不想输入用户名:chown-R $USER:$USER/home/michael/Desktop/react/
- 然后运行:
npm install node-sass
,不要使用sudo
注意:安装本地包时这很好,但对于全局安装,请阅读下面的更新
更新
虽然通过使用 chown
或 chmod
更改权限是很好的,但它不是安装软件包时的最佳方法全球,如果您使用的是 Linux 或 OSX,我建议您阅读关于 npm 文档的这篇文章 此处,并使用 nvm
,或手动更改 npm 的默认目录.>
更改 npm 的默认目录
为此,您需要按照以下步骤操作:
在命令行中,在您的主目录中,为全局安装创建一个目录:
mkdir ~/.npm-global
配置 npm 以使用新的目录路径:
npm 配置设置前缀 '~/.npm-global'
在您首选的文本编辑器中,打开或创建一个
~/.profile
文件 [或者您可以编辑~/.bashrc
或~/.zshrc
文件而不是.profile
如果你认为合适] 并添加这一行:export PATH=~/.npm-global/bin:$PATH
在命令行上,更新您的系统变量:
source ~/.profile# 或 source ~/.bashrc,# source ~/.zshrc,取决于你导出路径的位置
要测试您的新配置,请在不使用 sudo 的情况下全局安装软件包:
npm install -g jshint
你可以全局安装 node-sass :
npm install -g node-sass
现在您不应该收到权限错误,因为这应该可以解决全局安装软件包时的 EACCES 权限错误.
警告
LibSass
和 node-sass
已弃用.虽然他们将无限期地继续接收维护版本,但没有计划添加额外的功能或与任何新的 CSS 或 Sass 功能的兼容性,仍然使用它的项目应该转移到 Dart Sass.您可以在此处
请改用 sass,迁移到 Dart Sass 很简单并且这两个包都公开了相同的 JavaScript API,以安装 dart sass :
npm install sass
或全局:
npm install -g sass
Can't install the node-sass package via npm sudo npm i
, i have been trying to resolve via sudo npm i -g node-sass --unsafe-perm=true --allow-root
but it doesn't effect. Chmod 777 either.
I think there are something with npm permissions but i do not know
Log:
gyp verb build dir attempting to create "build" dir: /home/michael/Desktop/react/node_modules/node-sass/build
gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/home/michael/Desktop/react/node_modules/node-sass/build'
gyp ERR! System Linux 4.15.0-47-generic
gyp ERR! command "/usr/bin/node" "/home/michael/Desktop/react/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd /home/michael/Desktop/react/node_modules/node-sass
gyp ERR! node -v v11.14.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
Build failed with error code: 1
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.7 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.7: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-sass@4.11.0 postinstall: `node scripts/build.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-sass@4.11.0 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/michael/.npm/_logs/2019-04-13T17_21_20_424Z-debug.log
As a general recommendation, never run npm install with sudo as it will create local files owned by root instead of your user, i think you shoud try the following :
- First change the owner of the folder :
chown -R yourusername:yourusername /home/michael/Desktop/react/
, or if you don't want to type your username :chown -R $USER:$USER /home/michael/Desktop/react/
- then run :
npm install node-sass
, do not use sudo
NOTE : This is fine when you install local packages, but for global installations, read the update below
UPDATE
While changing the permissions by using chown
or chmod
is kinda fine and works, however it isn't the best approach when installing packages globally, if you are on a Linux or OSX i would suggest you read this article on the npm docs here, and either use nvm
, or manually change npm's default directory.
Changing npm's default directory
To do so, you need to follow these steps :
On the command line, in your home directory, create a directory for global installations:
mkdir ~/.npm-global
Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
In your preferred text editor, open or create a
~/.profile
file [alternatively you can edit either the~/.bashrc
or~/.zshrc
file instead of.profile
if you see fit] and add this line :export PATH=~/.npm-global/bin:$PATH
On the command line, update your system variables:
source ~/.profile # or source ~/.bashrc, # source ~/.zshrc, depends on where you exported the path
To test your new configuration, install a package globally without using sudo:
npm install -g jshint
You can install node-sass globally :
npm install -g node-sass
Now you shoudn't get permissions errors as this should resolve the EACCES permissions errors when installing packages globally .
Warning
LibSass
and node-sass
are deprecated. While they will continue to receive maintenance releases indefinitely, there are no plans to add additional features or compatibility with any new CSS or Sass features, projects that still use it should move onto Dart Sass.
You can read more on this topic here
Please use sass instead, migrating to Dart Sass is straightforward And both packages expose the same JavaScript API, to install dart sass :
npm install sass
Or globally :
npm install -g sass
这篇关于Npm 错误“错误:EACCES:权限被拒绝,mkdir '../node-sass/build'"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!