繁体   English   中英

无法加载配置“react”以从

[英]Failed to load config "react" to extend from

我正在尝试使用以下网站在不使用 create-react-app 的情况下创建一个反应应用程序:

https://www.freecodecamp.org/news/how-to-set-up-deploy-your-react-app-from-scratch-using-webpack-and-babel-a669891033d4/

但是,我不断收到这个错误,上面写着

WARNING in./src/index.js 模块警告(来自./node_modules/eslint-loader/dist/cjs.js):无法加载配置“react”以扩展自。

我已经尝试安装所有依赖项并花了几个小时在谷歌上搜索答案,但我似乎无法找出问题所在。

这是我的 package.json:

{
  "name": "xxx",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server --mode development",
    "format": "prettier --write \"src/**/*.js\"",
    "eslint-fix": "eslint --fix \"src/**/*.js\"",
    "build": "webpack --mode production"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@babel/plugin-proposal-class-properties": "^7.10.4",
    "react": "^16.13.1",
    "react-bootstrap": "^1.3.0",
    "react-dom": "^16.13.1"
  },
  "devDependencies": {
    "@babel/core": "^7.11.6",
    "@babel/preset-env": "^7.11.5",
    "@babel/preset-react": "^7.10.4",
    "babel-eslint": "^10.1.0",
    "babel-loader": "^8.1.0",
    "css-loader": "^4.3.0",
    "eslint": "^7.9.0",
    "eslint-config-react": "^1.1.7",
    "eslint-loader": "^4.0.2",
    "eslint-plugin-react": "^7.20.6",
    "html-webpack-plugin": "^4.4.1",
    "less": "^3.12.2",
    "less-loader": "^7.0.1",
    "prettier": "2.1.1",
    "style-loader": "^1.2.1",
    "webpack": "^4.44.1",
    "webpack-cli": "^3.3.12",
    "webpack-dev-server": "^3.11.0"
  }
}

这是我的 webpack.config.js:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
    entry: './src/index.js',
    output: {
      // path: __dirname + '/dist',
      path: path.resolve(__dirname, 'build'),
      publicPath: '/',
      filename: 'bundle.js'
    },
    devServer: {
      contentBase: './build'
    },
    module: {
      rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: ['babel-loader', 'eslint-loader']
      },
      {
        test: /\.less$/,
        use: [
          'style-loader',
          'css-loader',
          'less-loader',
        ],
      }
      ]
    },
    plugins: [
        new HtmlWebpackPlugin({
          template: path.resolve('./index.html'),
        })],
  };

这是我的.eslintrc:

  {
  "parser": "babel-eslint",
  "extends": "react",
  "env": {
    "browser": true,
    "node": true
  },
  "settings": {
    "react": {
      "version": "detect"
    }
  }
}

这是 my.babelrc:

{
    "presets": [
      "@babel/preset-env",
      "@babel/preset-react"
    ],
    "plugins": [
      "@babel/plugin-proposal-class-properties"
    ]
  }

有没有人有什么建议? 我正在绞尽脑汁想弄清楚这一点,但我遇到了很多麻烦。 它加载网页,但是根本没有使用反应。

先感谢您。! 我真的很感谢你的帮助。

我在create-react-app遇到了类似的问题,我做到了

yarn add eslint-config-react-app -D

我认为你应该尝试:

yarn add eslint-config-react -D

在对依赖项( react-scriptsreact ... )进行大升级后出现此错误,解决方案是删除yarn.lock文件

我正在阅读相同的教程并遇到了同样的问题。 我想出了几件事:

eslint-config-react已过时。 它已经 4 年没有更新了,所以我卸载了它,而是安装了(作为 devDependency) eslint-config-airbnb 还有其他的 eslint 配置包,但我听说 Airbnb 的 style guide 很受欢迎。 然后在我的 eslintrc 文件中,我将"extends": "react"替换为"extends": "airbnb"

{
  "parser": "babel-eslint",
  "extends": "airbnb",
  "env": {
    "browser": true,
    "node": true
  },
  "settings": {
    "react": {
      "version": "detect"
    }
  }
}

问题是eslint-config-react包。

留在这里以供将来参考:

就我而言,发生这种情况是因为我正在使用 Lerna 并且它正在提升一些必要的依赖项。 添加nohoist选项将lerna.json文件解决了这个问题对我来说:

"nohoist": [
  "*react*",
  "*react*/**",
  "*react-native*",
  "*react-native*/**"
],

我遇到了同样的问题。 我做了这些步骤,它奏效了。

将其安装为依赖项

npm i eslint-config-react-app

然后在您的根文件夹中,创建一个文件名.eslintrc.json并将此代码添加到该文件中

  "extends": "react-app"

我修复它手动安装eslint-config-react-app包:

yarn add eslint-config-react-app -D

# or

npm install -D eslint-config-react-app

当我删除

  "extends": [
      "react-app"
    ]
  },

package.json它工作。

这是因为eslint-config-react-app包已经过时了。

如果您从 GitHub 或任何其他版本控制系统克隆了 repo,请尝试将此文件.eslintrc.json添加到您的根文件夹。 不要忘记. 一开始。

第一个答案也很有魅力。 只是想我也会添加我的。

如果您使用的是 Yarn 2(浆果)

在 .yarnrc.yml 文件中添加 nodeLinker: node-modules

https://yarnpkg.com/getting-started/install 中所述

在收到消息“Failed to load config ‘react-app’ to extend from... 我从 package.json 中删除了以下几行:

    "eslintConfig": {
    "extends": [
      "react-app"
    ]
  },

它起作用了

要解决此问题,您需要在项目文件夹中运行此命令 第一步:

yarn set version berry

运行后,将在您的本地文件夹(项目文件夹)中生成一个文件和文件夹:

  • .yarnrc.yml(文件)
  • .yarn(文件夹)

第二步:在 .yarnrc.yml 文件中添加nodeLinker: node-modules

第三步:运行此命令yarn install

最后运行yarn start

提示:当您想从 yarn 1.x 迁移到 2.x 或 3x 或更高版本时,此方法很有用。

甚至我也遇到了这个问题,列出了解决这个问题的步骤。

  1. 首先删除节点模块并重新安装节点模块。
  2. 然后我在运行 npm run build 时遇到了一个问题,我得到了react-scripts not found 所以我使用 npm 安装了 react-scripts
  3. 然后我遇到了这个问题,无法加载配置 react-app 来扩展 form 我用

npm 添加 eslint-config-react-app

如果您使用纱线:

1- 删除:node_modules 和 yarn.lock
2-运行:纱线安装

注意:对于 npm 应该是同样的方法。

当我尝试将我的 create-react-app 模板部署到 Vercel 时,我遇到了同样的错误。

  1. 删除 yarn.lock 文件
  2. (在 Vercel 仪表板上)将构建命令从 auto 覆盖到“npm run build”
  3. (在 Vercel 仪表板上)将安装命令从 auto 覆盖到“npm install”

帮助我成功部署。

如果您的依赖项中有eslint ,请将其删除。
将使用来自react-scriptseslint

"eslint": "^7.32.0"

也有这个问题。 如果有人发现自己处于这种困境并且您正在分叉一个项目,请删除包锁为我解决了这个问题。

我有同样的问题。

  • 我删除了 package-lock.json 和 node_modules
  • 运行“npm 安装”

它工作得很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM