gpt4 book ai didi

reactjs - 错误 : Node Sass version 5. 0.0 与 ^4.0.0 不兼容

转载 作者:行者123 更新时间:2023-12-03 07:26:23 31 4
gpt4 key购买 nike

我创建了一个空白的 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.
    包.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;博士

  • npm uninstall node-sass
  • npm install node-sass@4.14.1

  • 或者,如果使用 yarn (较新的 CRA 版本中的默认值)
  • yarn remove node-sass
  • yarn add node-sass@4.14.1

  • 编辑 2 : 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。
    他们的存储库中有一个 Unresolved 问题,需要审查相关的修复程序。在此之前,请参阅下面的解决方案。

    解决方法 : 暂时不要安装 node-sass 5.0.0(主要版本刚刚被撞)。
    卸载 node-sass npm uninstall node-sass然后安装最新版本(5.0之前) npm install node-sass@4.14.1
    注意:LibSass(因此也是 node-sass)是 deprecated dart-sass 是推荐的实现。 You can use sass instead ,它是 dart-sass 编译为纯 JavaScript 的节点分布。
    但请注意:

    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

    关于reactjs - 错误 : Node Sass version 5. 0.0 与 ^4.0.0 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64625050/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com