错误:Node Sass版本5.0.0与^ 4.0.0不兼容 [英] Error: Node Sass version 5.0.0 is incompatible with ^4.0.0

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

问题描述

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

已安装:

  • 反应v17.0.1,
  • node-sass v5.0.0,

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

App.js

  import'./App.scss'函数App(){返回 (< div className =" App">应用程序</div>);}导出默认应用程序; 

抛出错误:

 无法编译../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&","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卸载node-sass
  2. npm install node-sass@4.14.1

或者,如果使用纱线(新的CRA版本中的默认设置)

  1. yarn删除节点无效
  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.semver不匹配,因为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 ,它是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 v15.0.1

Installed:

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

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

App.js

import './App.scss'

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

export default App;

Throw 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.

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. 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 an so on.
The recommendation for now is: if you're installing just node-sass check 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 latest sass-loader.


Edit: this error comes from sass-loader. There is a semver 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 (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 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天全站免登陆