zoukankan      html  css  js  c++  java
  • node-sass安装失败的问题 stack Error: EACCES: permission denied, mkdir

    最近在自己的项目中遇到了一个node-sass 包安装的问题,问题如下
    当我 sudo npm i 会出现下面这段错误信息,我试了修改权限和修改包的版本后还是没有能够解决,最后google 终于解决了,这里记下问题的原因和解决办法

    gyp verb build dir attempting to create "build" dir: /工作:学习/github/react-view/node_modules/node-sass/build
    gyp ERR! configure error 
    gyp ERR! stack Error: EACCES: permission denied, mkdir '/工作:学习/github/react-view/node_modules/node-sass/build'
    gyp ERR! System Darwin 17.7.0
    gyp ERR! command "/usr/local/bin/node" "/工作:学习/github/react-view/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
    gyp ERR! cwd /工作:学习/github/react-view/node_modules/node-sass
    gyp ERR! node -v v10.15.3
    gyp ERR! node-gyp -v v3.8.0
    gyp ERR! not ok 
    

    首先造成这个问题的原因是:
    npm会有生命周期,某个包会有生命周期来执行一些东西,安全起见会自动降级导致没有权限执行一些操作,通过--unsafe-perm参数来解锁该限制。
    npm lifecycle 命令在执行前,会判断配置unsafe-perm为true 时才继续,否则会提前退出。

     // lib/utils/lifecycle.js
        unsafe = unsafe || npm.config.get('unsafe-perm')
        if ((wd.indexOf(npm.dir) !== 0 || _incorrectWorkingDirectory(wd, pkg)) && !unsafe && pkg.scripts[stage]) {
          log.warn('lifecycle', logid(pkg, stage), 'cannot run in wd',
            '%s %s (wd=%s)', pkg._id, pkg.scripts[stage], wd
          )
          return cb()
        }
    

    配置的读取顺序大致参考:npm-config,即cli -> env -> npmrc -> default。

    default中关于unsafe-perm的初始化如下:

    // lib/config/defaults.js
        'unsafe-perm': process.platform === 'win32' ||
                         process.platform === 'cygwin' ||
                         !(process.getuid && process.setuid &&
                           process.getgid && process.setgid) ||
                         process.getuid() !== 0
    

    针对unix 平台,使用root 用户执行npm 命令时得到的默认值都会是false。

    解决办法

    • 安装时临时修改npm配置 sudo npm install --unsafe-perm
    • 设置项目中npm的默认配置 npm config set unsafe-perm=true
    • 在项目中创建.npmrc来覆盖default的配置
    // .npmrc
    unsafe-perm = true
    
    • 全局设置 npm config -g set unsafe-perm
    • 这里就需要修改node目录的权限为root
    chown root:root node
    
    • npm提供的解决方案
     ## 创建.npm-global目录
     mkdir ~/.npm-global
     ## 设置
     npm config set prefix '~/.npm-global'
     export PATH=~/.npm-global/bin:$PATH
     source ~/.profile
    

    参考:

  • 相关阅读:
    nginx实现请求的负载均衡 + Keep Alive实现nginx的高可用
    理解什么是JWT(Json web token)及Python实现
    TCP/UDP协议到底是什么
    Redis实现分布式单点登录
    Python面试题---给定一个字符串 {xxx[xxx{xxx}]xx{x[xxx]xxx{xxx}xx}x} 判断其中的 {}[]() 是否成对出现
    Typora里面如何快捷改变字体颜色?
    基于Docker安装关系型数据库PostgrelSQL替代Mysql
    PEP8-Python编码规范
    欢迎来到我的友链小屋
    windows下lib和dll区别
  • 原文地址:https://www.cnblogs.com/chrissong/p/10961521.html
Copyright © 2011-2022 走看看