错误:'node-sass' 版本 5.0.0 与 ^4.0.0 不兼容 [英] Error: 'node-sass' version 5.0.0 is incompatible with ^4.0.0

查看:71
本文介绍了错误:'node-sass' 版本 5.0.0 与 ^4.0.0 不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个空白的 React 项目,在 npm v7.0.7 和 Node.js v15.0.1 上使用命令:npx create-react-app

已安装:

  • React v17.0.1,
  • node-sass v5.0.0,

然后我尝试将一个空白的 .scss 文件导入到 App 组件中:

文件 App.js

import './App.scss'功能应用(){返回 (

应用程序

);}导出默认应用程序;

它抛出一个错误:

编译失败../src/App.scss (./node_modules/css-loader/dist/cjs.js??ref--5-oneOf-6-1!./node_modules/postcss-loader/src??postcss!./node_modules/resolve-url-loader??ref--5-oneOf-6-3!./node_modules/sass-loader/dist/cjs.js??ref--5-oneOf-6-4!./src/App.scss)错误:Node Sass 5.0.0 版与 ^4.0.0 不兼容.

文件 package.json

<代码>{名称":react-17-node-sass-5",版本":0.1.0",私人":真的,依赖关系":{@testing-library/jest-dom":^5.11.5",@testing-library/react":^11.1.0",@testing-library/user-event":^12.1.10",节点-sass":^5.0.0",反应":^ 17.0.1",react-dom":^17.0.1",反应脚本":4.0.0",网络生活":^0.2.4"},...}}

解决方案

TL;DR

  1. npm uninstall node-sass
  2. npm install node-sass@4.14.1

或者,如果使用 Yarn(在较新的 CRA 版本)

  1. yarn remove node-sass
  2. yarn add node-sass@4.14.1


Edit2:sass-loader v10.0.5 修复了它.问题是您可能没有将它用作项目依赖项,而是更多地将其用作依赖项的依赖项.CRA 使用固定版本,angular-cli 锁定到 node-sass v4,等等.

目前的建议是:如果您只安装 node-sass,请检查以下解决方法(和注释).如果您正在处理一个空白项目,并且您可以管理您的 Webpack 配置(不使用 CRA 或一个 CLI 来搭建你的项目),安装最新的 sass-loader.


编辑:此错误来自 sass-loader.存在 语义版本 不匹配,因为 node-sass @latest 是 v5.0.0,而 sass-loader 需要 ^4.0.0.

他们的存储库中存在一个未解决的问题,其中包含需要审查的相关修复程序.在此之前,请参考以下解决方案.


解决方法:不要安装 node-sass 5.0.0(主要版本刚刚被撞).

卸载 node-sass

npm 卸载 node-sass

然后安装最新版本(5.0之前)

npm install node-sass@4.14.1


注意:LibSass(因此也是 node-sass)已弃用和dart-sass 是推荐的实现.您可以使用 sass 代替,这是一个 Node.js 发行版dart-sass 编译为纯 JavaScript.

请注意:

<块引用>

小心使用这种方法.React-scripts 使用 sass-loader v8,它更喜欢 node-sass 而不是 sass(它有一些 node-sass 不支持的语法).如果两者都安装并且用户使用 sass,这可能会导致 CSS 编译错误

I've created a blank React project, using the command: npx create-react-app on npm v7.0.7 and Node.js v15.0.1

Installed:

  • React v17.0.1,
  • node-sass v5.0.0,

Then I tried to import a blank .scss file to the App component:

File App.js

import './App.scss'

function App() {
  return (
    <div className="App">
      App
    </div>
  );
}

export default App;

It throws an error:

Failed to compile.

./src/App.scss (./node_modules/css-loader/dist/cjs.js??ref--5-oneOf-6-1!./node_modules/postcss-loader/src??postcss!./node_modules/resolve-url-loader??ref--5-oneOf-6-3!./node_modules/s
ass-loader/dist/cjs.js??ref--5-oneOf-6-4!./src/App.scss)
Error: Node Sass version 5.0.0 is incompatible with ^4.0.0.

File package.json

{
  "name": "react-17-node-sass-5",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.5",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "node-sass": "^5.0.0",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-scripts": "4.0.0",
    "web-vitals": "^0.2.4"
  },
 ...

  }
}

解决方案

TL;DR

  1. npm uninstall node-sass
  2. npm install node-sass@4.14.1

Or, if using Yarn (default in newer CRA versions)

  1. yarn remove node-sass
  2. yarn add node-sass@4.14.1


Edit2: sass-loader v10.0.5 fixes it. The problem is you might not be using it as a project dependency, but more as a dependency of your dependencies. CRA uses a fixed version, angular-cli locks to node-sass v4, and so on.

The recommendation for now is: if you're installing just node-sass, check the below workaround (and the note). If you're working on a blank project and you can manage your Webpack configuration (not using CRA or a CLI to scaffold your project), install the latest sass-loader.


Edit: this error comes from sass-loader. There is a semantic versioning mismatch since node-sass @latest is v5.0.0 and sass-loader expects ^4.0.0.

There is an open issue on their repository with an associated fix that needs to be reviewed. Until then, refer to the solution below.


Workaround: don't install node-sass 5.0.0 yet (the major version was just bumped).

Uninstall node-sass

npm uninstall node-sass

Then install the latest version (before 5.0)

npm install node-sass@4.14.1


Note: LibSass (hence node-sass as well) is deprecated and dart-sass is the recommended implementation. You can use sass instead, which is a Node.js distribution of dart-sass compiled to pure JavaScript.

Be warned though:

Be careful using this approach. React-scripts uses sass-loader v8, which prefers node-sass to sass (which has some syntax not supported by node-sass). If both are installed and the user worked with sass, this could lead to errors on CSS compilation

这篇关于错误:'node-sass' 版本 5.0.0 与 ^4.0.0 不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆