当我在 Django 项目中设置 React 时,我遇到了这个错误
ModuleBuildError in Module build failed (from ./node_modules/babel-loader/lib/index.js): SyntaxError: C:\Users\1Sun\Cebula3\cebula_react\assets\js\index.js: Support for the experimental syntax 'classProperties ' 当前未启用 (17:9):
15 |
16 | class BodyPartWrapper extends Component {
> 17 | state = {
| ^
18 |
19 | }
20 |
Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the
'plugins' section of your Babel config to enable transformation.
所以,我安装了@babel/plugin-proposal-class-properties 并将其放入 babelrc
包.json
{
"name": "cebula_react",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --config ./webpack.config.js --mode development",
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --config prod.config.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-react"
]
},
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"react-hot-loader": "^4.3.6",
"webpack": "^4.17.2",
"webpack-bundle-tracker": "^0.3.0",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.8"
},
"dependencies": {
"react": "^16.5.0",
"react-dom": "^16.5.0"
}
}
babelrc
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
但是错误仍然存在,是什么问题?
@babel/plugin-proposal-class-properties
和 babel-plugin-transform-class-properties
。安装后要重建,是吗?
npx babel-upgrade --write --install
改变
"plugins": [
"@babel/plugin-proposal-class-properties"
]
至
"plugins": [
[
"@babel/plugin-proposal-class-properties",
{
"loose": true
}
]
]
这对我有用
首先安装:@babel/plugin-proposal-class-properties 作为开发依赖:
npm install @babel/plugin-proposal-class-properties --save-dev
然后编辑你的 .babelrc ,让它完全像这样:
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
[
"@babel/plugin-proposal-class-properties"
]
]
}
.babelrc 文件位于 package.json 所在的根目录中。
请注意,您应该重新启动 webpack 开发服务器以使更改生效。
webpack项目解决方案
我只是通过将 @babel/plugin-proposal-class-properties
添加到 webpack 配置插件中来解决这个问题。我的 webpack.config.js
的模块部分如下所示
module: {
rules: [
{
test: path.join(__dirname, '.'),
exclude: /(node_modules)/,
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env',
'@babel/react',{
'plugins': ['@babel/plugin-proposal-class-properties']}]
}
}
]
}
webpack.config.js
是哪一个?我目前得到 3 个。
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
[
"@babel/plugin-proposal-class-properties"
]
]
}
用上面的代码替换你的 .babelrc 文件。它为我解决了这个问题。
npm install --save-dev @babel/preset-env @babel/preset-react @babel/plugin-proposal-class-properties
@babel/plugin-proposal-class-properties
依赖项。
在我的工作环境根目录中,.babelrc 文件不存在。但是,在 package.json 中的以下条目解决了该问题。
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]}
注意:在执行 npm 或 yarn 命令之前,不要忘记退出控制台并重新打开。
有两种处理反应状态的方法:
选项 1:只需添加到 package.json:
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
选项 2:
1. 在根文件夹中创建一个名为 .babelrc 的文件。
在 .babelrc 中写入:
{ "plugins": ["@babel/plugin-proposal-class-properties"] }
跑:
npm i @babel/plugin-proposal-class-properties
3.运行:
npm run dev
or
npm run watch
经过近 3 个小时的搜索并在同一个错误上花费时间后,我发现我正在为 React 使用名称导入:
import { React } from 'react';
这是完全错误的。只需将其切换为:
import React from 'react';
所有错误都消失了。我希望这可以帮助别人。这是我的 .babelrc:
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
webpack.config.js
const path = require('path');
const devMode = process.env.Node_ENV !== 'production';
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
entry: './src/App.js',
devtool: 'source-map',
output: {
path: path.resolve(__dirname, 'public'),
filename: 'App.js'
},
mode: 'development',
devServer: {
contentBase: path.resolve(__dirname, 'public'),
port:9090,
open: 'google chrome',
historyApiFallback: true
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},{
test: /\.(sa|sc|c)ss$/,
use: [
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
modules: true,
localIdentName: '[local]--[hash:base64:5]',
sourceMap: true
}
},{
loader: 'sass-loader'
}
]
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: devMode ? '[name].css' : '[name].[hash].css',
chunkFilename: devMode ? '[id].css' : '[id].[hash].css'
})
]
}
package.json
{
"name": "expense-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack",
"serve": "webpack-dev-server"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/cli": "^7.1.2",
"@babel/core": "^7.1.2",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.4",
"css-loader": "^1.0.0",
"mini-css-extract-plugin": "^0.4.3",
"node-sass": "^4.9.3",
"react-router-dom": "^4.3.1",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"webpack": "^4.20.2",
"webpack-cli": "^3.1.2",
"webpack-dev-server": "^3.1.9"
},
"dependencies": {
"normalize.css": "^8.0.0",
"react": "^16.5.2",
"react-dom": "^16.5.2"
}
}
将 state
移入 constructor function
对我有用:
...
class MyComponent extends Component {
constructor(man) {
super(man)
this.state = {}
}
}
...
祝你好运...
安装 plugin-proposal-class-properties npm install @babel/plugin-proposal-class-properties --save-dev
通过添加 'plugins' 来更新你的 webpack.config.js: ['@babel/plugin-proposal-class-properties']}]
Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema. - configuration.plugins[1] should be one of these: object { apply, … } | function -> Plugin of type object or instanceof Function Details: * configuration.plugins[1] should be an object. -> Plugin instance * configuration.plugins[1] should be an instance of function -> Function acting as plugin
的错误
我发现我的 .babelrc
被忽略的问题,但是我创建 babel.config.js
并添加以下内容:
module.exports = {
plugins: [
['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties', { loose: true }],
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-transform-regenerator',
[
'@babel/plugin-transform-runtime',
{
helpers: false,
regenerator: true,
},
],
],
presets: [
"@babel/preset-flow",
'module:metro-react-native-babel-preset',
],
};
它适用于 React Native 应用程序,我认为这也将有助于 React 应用程序。
module.exports = { "presets": ["module:metro-react-native-babel-preset"], "plugins": ["@babel/plugin-proposal-class-properties"] }
对我来说已经足够了。你能更新你的答案吗?我们也应该理解为什么 .babelrc
被忽略了
根据 this GitHub 问题,如果您使用 create-react-app,您应该将您的 .babelrc
或 babel.config.js
配置复制到 webpack.config.js
并删除这些配置。因为 htis 有两行代码 babelrc: false,configFile: false,
您在 babelrc 中的配置, ..没用。并且您的 webpack.config.js
在您的 ./node_madules/react-scripts/config
文件夹中,我这样解决了我的问题:
{
test: /\.(js|mjs)$/,
exclude: /@babel(?:\/|\\{1,2})runtime/,
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
configFile: false,
compact: false,
presets: [
[
require.resolve('babel-preset-react-app/dependencies'),
{ helpers: true },
],
'@babel/preset-env', '@babel/preset-react'
],
plugins: ['@babel/plugin-proposal-class-properties'],
.
.
.
我明确地使用了 babel 解析器。以上解决方案都不适合我。这行得通。
const ast = parser.parse(inputCode, {
sourceType: 'module',
plugins: [
'jsx',
'classProperties', // '@babel/plugin-proposal-class-properties',
],
});
我正在使用纱线。我必须执行以下操作来克服错误。
yarn add @babel/plugin-proposal-class-properties --dev
添加
"plugins": [
[
"@babel/plugin-proposal-class-properties"
]
]
进入 .babelrc
对我有用。
yarn add --dev @babel/plugin-proposal-class-properties
或者
npm install @babel/plugin-proposal-class-properties --save-dev
.babelrc
对于弹出的 create-react-app 项目
我刚刚解决了我的案例,在我的 webpack.config.js
中添加了以下几行:
presets: [
[
require.resolve('babel-preset-react-app/dependencies'),
{ helpers: true },
],
/* INSERT START */
require.resolve('@babel/preset-env'),
require.resolve('@babel/preset-react'),
{
'plugins': ['@babel/plugin-proposal-class-properties']
}
/* INSERTED END */
],
如果有人在 react-native-web-monorepo 之后处理 monorepo,那么您需要在 packages/web
中提交 config-overrides.js
文件。您需要在该文件中添加 resolveApp('../../node_modules/react-native-ratings'),
...
我完整的 config-override.js
文件是
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
// our packages that will now be included in the CRA build step
const appIncludes = [
resolveApp('src'),
resolveApp('../components/src'),
resolveApp('../../node_modules/@react-navigation'),
resolveApp('../../node_modules/react-navigation'),
resolveApp('../../node_modules/react-native-gesture-handler'),
resolveApp('../../node_modules/react-native-reanimated'),
resolveApp('../../node_modules/react-native-screens'),
resolveApp('../../node_modules/react-native-ratings'),
resolveApp('../../node_modules/react-navigation-drawer'),
resolveApp('../../node_modules/react-navigation-stack'),
resolveApp('../../node_modules/react-navigation-tabs'),
resolveApp('../../node_modules/react-native-elements'),
resolveApp('../../node_modules/react-native-vector-icons'),
];
module.exports = function override(config, env) {
// allow importing from outside of src folder
config.resolve.plugins = config.resolve.plugins.filter(
plugin => plugin.constructor.name !== 'ModuleScopePlugin'
);
config.module.rules[0].include = appIncludes;
config.module.rules[1] = null;
config.module.rules[2].oneOf[1].include = appIncludes;
config.module.rules[2].oneOf[1].options.plugins = [
require.resolve('babel-plugin-react-native-web'),
require.resolve('@babel/plugin-proposal-class-properties'),
].concat(config.module.rules[2].oneOf[1].options.plugins);
config.module.rules = config.module.rules.filter(Boolean);
config.plugins.push(
new webpack.DefinePlugin({ __DEV__: env !== 'production' })
);
return config
};
我在尝试用 babel 转换一些 jsx 时遇到了同样的问题。以下是对我有用的解决方案。您可以将以下 json 添加到您的 .babelrc
{
"presets": [
[
"@babel/preset-react",
{ "targets": { "browsers": ["last 3 versions", "safari >= 6"] } }
]
],
"plugins": [["@babel/plugin-proposal-class-properties"]]
}
对于带有 webpack 的 react 项目:
执行: npm install @babel/plugin-proposal-class-properties --save-dev 在 package.json 和 webpack.config.js 所在的根文件夹中创建 .babelrc(如果不存在)文件,并添加以下代码:
{“预设”:[“@babel/preset-env”,“@babel/preset-react”],“插件”:[[“@babel/plugin-proposal-class-properties”,{“松散”:真} ] ] }
将以下代码添加到 webpack.config.js 文件中:
{测试:/\.jsx?$/,排除:/node_modules/,加载器:'babel-loader',查询:{预设:[“@babel/preset-env”,“@babel/preset-react”]} , 解决: { 扩展名: ['.js', '.jsx'] } }
关闭终端并再次运行 npm start。
你必须安装
npm install @babel/core @babel/plugin-proposal-class-properties @babel/preset-env @babel/preset-react babel-loader
和
更改输入和输出
const path = require('path')
module.exports = {
entry: path.resolve(__dirname,'src', 'app.js'),
output: {
path: path.resolve(__dirname, "public","dist",'javascript'),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.(jsx|js)$/,
exclude: /node_modules/,
use: [{
loader: 'babel-loader',
options: {
presets: [
['@babel/preset-env', {
"targets": "defaults"
}],
'@babel/preset-react'
],
plugins: [
"@babel/plugin-proposal-class-properties"
]
}
}]
}
]
}
}
我为 src/components 创建了一个符号链接 -> ../../components 导致 npm start
发疯并停止将 src/components/*.js 解释为 jsx,从而给出相同的错误。来自官方 babel 的所有修复它的说明都是无用的。当我复制回组件目录时,一切都恢复正常!
确保您没有错误地导入 import BrowserRouter from "react-router-dom/modules/BrowserRouter";
而不是 import {BrowserRouter} from "react-router-dom";
如果在 typescript 更新后发生这种情况,只需将 useDefineForClassFields: false
添加到 tsconfig 文件。
请参阅:https://www.typescriptlang.org/tsconfig#useDefineForClassFields
.babelrc
重命名为babel.config.js
并使用module.export
,如 github 上所讨论的 stackoverflow.com/questions/53916434/… github.com/babel/babel/issues/7879#issuecomment-419732313Test suite failed to run; .loose is not a valid Plugin property
{"loose": true}
}