Помилка: Node Sass версії 5.0.0 несумісний з ^ 4.0.0


124

Я створив порожній проект React, використовуючи команду: npx create-react-appна npm v7.0.7 та Node v15.0.1

Встановлено:

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

Потім я спробував імпортувати порожній файл .scss до компонента програми:

App.js

import './App.scss'

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

export default App;

Викинути помилку:

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"
  },
 ...
 
  }
}

Відповіді:


197

TL;DR

  1. npm uninstall node-sass
  2. npm install 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


3
for some reason yarn add node-sass@4.14.1 is much quicker
Tylik Stec

1
Looks like they just fixed that in github.com/webpack-contrib/sass-loader/commit/… , so you can install sass-loader@10.0.5
Marcin

1
@TylikStec yarn is another package manager, but don't use both in the same project, having two lock files may cause issues in deployment
Binara Medawatta

5
@marcin it's funny that you say "they" since the person who posted the answer is the one who fixed it :]
Benjamin Gruenbaum

Working on gatsby for me. Thanks!
Diego Fortes

10

Uninstall node-sass

npm uninstall node-sass

use sass by:

npm install -g sass
npm install --save-dev sass

This worked for me, thank you.
dro3333

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.
Nicolas Hevia

3

The only one reason why you get some error like that, it's because your node version is not compatible with your node-sass version.

So, make sure to checkout the documentation at here: https://www.npmjs.com/package/node-sass

Or this image below will be help you, what the node version can use the node-sass version.

enter image description here

For an example, if you're using node version 12 on your windows ("maybe"), then you should have to install the node-sass version 4.12.

npm install node-sass@4.12

Yeah, like that. So now you only need to install the node-sass version recommended by the node-sass team with the nodes installed on your computer.


With this approach you're locked to v4.x, instead of having the latest version of v4. For example node 12 would be stuck with 4.12 even if it supports 4.14. npm update node-sass would not work unless you used npm install node-sass@^4.12 which is the same than installing 4.14.1 (latest known version of v4).
Nicolas Hevia

Thank's for your response Nicolas Hevia, but I've been try that code above and you're right, it's can't update that version of node-sass if I'm use npm install node-sass@4.12. But I've been try with node-sass 4.14.1 in nodejs 12.18.3 and it's not working. That's why I recomended to use the version what the node-sass team recomended and I'm not use ^. Because it will update to the latest version of node-sass 4
Titus Sutio Fanpula

0

If you happen to use CRA with default yarn package manager use the following. Worked for me.

yarn remove node-sass 
yarn add node-sass@4.14.1
Використовуючи наш веб-сайт, ви визнаєте, що прочитали та зрозуміли наші Політику щодо файлів cookie та Політику конфіденційності.
Licensed under cc by-sa 3.0 with attribution required.