关于安装 node-Sass 报错的解决记录

1,521 阅读2分钟

关于安装 node-Sass 报错的解决记录(转载的,侵删)

问题

新建 项目后 yarn 或 npm install 命令的时候,node-sass报错了

解决

  • 查看 npm 配置 npm config list

    1. 将 npm 源切换至淘宝源
      npm config set registry https://registry.npm.taobao.org

    2. 通过 npm 全局安装 yarn
      npm install -g yarn

      如果执行 yarn 命令遇到报错,请将 npm bin -g 所得的路径添加至 PATH 环境变量中。

  • 查看 yarn 配置 yarn config list

    1. 将 yarn 源切换至淘宝源
      yarn config set registry https://registry.npm.taobao.org
    2. 执行 yarn install alias yarn
  • 配置 nvm 相关镜像地址

    nvm node_mirror http://npm.taobao.org/mirrors/node/
    nvm npm_mirror http://npm.taobao.org/mirrors/npm/
    

报错分类

1. error An unexpected error occurred

如果遇到错误 error An unexpected error occurred: “EINVAL: invalid argument, symlink

> 请在你的执行命令之后添加 `--no-bin-links`\
> e.g.\
> yarn install –no-bin-links

2. node-sass: Command failed

如果遇到错误 error: xxxx node-sass: Command failed
将 sass-binary-site 添加至 config 中

     yarn config set sass-binary-site https://npm.taobao.org/mirrors/node-sass
     npm config set sass-binary-site https://npm.taobao.org/mirrors/node-sass

指定 node-sass 从 npm 的淘宝源中下载。

3. error in ./resources/assets/sass/app.scss

如果遇到错误

> ERROR Failed to compile with 2 errors 11:10:34\
> error in ./resources/assets/sass/app.scss

1.  检查你使用的源是否更换,当然可以选择使用代理 ![:joy:](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/feeec2dc16fb4189b6c3f7cdbf0a260a~tplv-k3u1fbpfcp-zoom-1.image)
2.  检查 sass-binary-site 是否指定
3.  执行 `yarn install --force node-sass` npm 对应的是 `npm rebuild node-sass`

*我习惯直接 `yarn install --force` 将整个重新进行构建*

4. 无法执行命令

如果你使用 yarn 安装了 vue 这样的 cli 工具。然后无法执行命令。请将 yarn global bin 添加至 PATH 环境变量中。

若任然无法安装 node-sass ,可手动指定环境变量 SASS_BINARY_PATH,windows 参考如下:

SASS_BINARY_PATH 环境变量配置

5. 如果遇到 cross-env 报错

`npm i -g cross-env`

6. 其他情况

如果仍然不能下载 参考文章:www.jianshu.com/p/4609564e3…
参考 issue:github.com/yarnpkg/yar…

创建 .yarnrc 文件,并放置在项目中

registry="https://registry.npm.taobao.org"

sass_binary_site="https://npm.taobao.org/mirrors/node-sass/"
phantomjs_cdnurl="http://cnpmjs.org/downloads"
electron_mirror="https://npm.taobao.org/mirrors/electron/"
sqlite3_binary_host_mirror="https://foxgis.oss-cn-shanghai.aliyuncs.com/"
profiler_binary_host_mirror="https://npm.taobao.org/mirrors/node-inspector/"
chromedriver_cdnurl="https://cdn.npm.taobao.org/dist/chromedriver"

版本对照

NodeJS  | Supported node-sass version | Node Module
--------|-----------------------------|------------
Node 20 | 9.0+                        | 115
Node 19 | 8.0+                        | 111
Node 18 | 8.0+                        | 108
Node 17 | 7.0+, <8.0                  | 102
Node 16 | 6.0+                        | 93
Node 15 | 5.0+, <7.0                  | 88
Node 14 | 4.14+, <9.0                 | 83
Node 13 | 4.13+, <5.0                 | 79
Node 12 | 4.12+, <8.0                 | 72
Node 11 | 4.10+, <5.0                 | 67
Node 10 | 4.9+, <6.0                  | 64
Node 8  | 4.5.3+, <5.0                | 57
Node <8 | <5.0                        | <57

引用:www.jianshu.com/p/89f5e094b…

如果提示无权限保存二进制文件,可执行 npm install --unsafe-perm