美文网首页我爱编程
Mac下安装项目npm依赖包失败

Mac下安装项目npm依赖包失败

作者: 好吃的野菜 | 来源:发表于2018-04-05 07:01 被阅读41次

    Mac下安装项目依赖包,报以下错误:

    image.png

    在github上找到了Bug Report or Feature Request (mark with an x) ,是在angular/cli项目中提的issue, @filipesilva在该问题下的回答:

    This seems to be a problem with installing https://github.com/sass/node-sass, which is one of the dependencies.
    The problem most likely arises from the use of sudo, which is heavily discouraged while installing packages. Check out https://docs.npmjs.com/getting-started/fixing-npm-permissions to see how you can fix your permissions.

    fixing-npm-permissions 这个链接中给出了两种解决方案:
    1. Reinstall with a Node Version Manager (使用node版本管理器重新安装node,比如nvm)
    a. 先卸载之前安装的node/npm (mac下需要使用sudo)

    sudo rm -rf /usr/local/lib/node_modules  # 删除全局 node_modules 目录
    sudo rm /usr/local/bin/node # 删除 node
    cd  /usr/local/bin && ls -l | grep "../lib/node_modules/" | awk '{print $9}'| xargs rm # 删除全局 node 模块注册的软链
    

    b. 安装nvm (适用于mac)

     curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
    

    详见:[https://github.com/creationix/nvm/blob/master/README.md]
    安装完 nvm 后,输入nvm,当看到有输出时,则 nvm 安装成功。若没安装成功,则需要做以下操作:

      vi .bash_profile
    

    然后将以下代码复制进去,保存退出

    export NVM_DIR="$HOME/.nvm"
    export NVM_NODEJS_ORG_MIRROR="http://npm.taobao.org/mirrors/node"
    [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
    

    然后 source 一下 .bash_profile

    source .bash_profile
    

    c. 使用nvm安装node

    nvm install 8.9.1  # 安装 node8.9.1
    

    2. Change npm's Default Directory (变更已安装的npm的默认目录)
    a. Back-up your computer before you start.
    Make a directory for global installations:

    mkdir ~/.npm-global
    

    Configure npm to use the new directory path:

    npm config set prefix '~/.npm-global'
    

    Open or create a ~/.profile file and add this line:

    export PATH=~/.npm-global/bin:$PATH
    

    Back on the command line, update your system variables:

    source ~/.profile
    

    Test: Download a package globally without using sudo.

    npm install -g jshint
    

    Instead of steps 2-4, you can use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile):

    NPM_CONFIG_PREFIX=~/.npm-global
    

    ⚠️本系列文章只是简单记录,提供解决问题的思路,不保证其中所有步骤都可以如预期般有效!
    作者使用第一种方案,安装nvm node管理器重新安装node,解决了此问题。

    相关文章

      网友评论

        本文标题:Mac下安装项目npm依赖包失败

        本文链接:https://www.haomeiwen.com/subject/ppkrhftx.html