Reactjs 支持实验语法';classProperties';isn';t当前已启用
在Django项目中设置React时,我遇到了这个错误 中的模块构建错误 模块构建失败(来自./node_modules/babel loader/lib/index.js): 语法错误:C:\Users\1Sun\Cebula3\cebula\u react\assets\js\index.js:Support 对于实验语法,“classProperties”当前未启用(17:9): 所以,我安装了@babel/plugin建议类属性,并将其放在babelrc中 package.jsonReactjs 支持实验语法';classProperties';isn';t当前已启用,reactjs,webpack,babeljs,Reactjs,Webpack,Babeljs,在Django项目中设置React时,我遇到了这个错误 中的模块构建错误 模块构建失败(来自./node_modules/babel loader/lib/index.js): 语法错误:C:\Users\1Sun\Cebula3\cebula\u react\assets\js\index.js:Support 对于实验语法,“classProperties”当前未启用(17:9): 所以,我安装了@babel/plugin建议类属性,并将其放在babelrc中 package.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"
]
}
{
"presets": [
[
"@babel/preset-react",
{ "targets": { "browsers": ["last 3 versions", "safari >= 6"] } }
]
],
"plugins": [["@babel/plugin-proposal-class-properties"]]
}
但是错误仍然存在,问题是什么??更改
"plugins": [
"@babel/plugin-proposal-class-properties"
]
至
"plugins": [
[
"@babel/plugin-proposal-class-properties",
{
"loose": true
}
]
]
这对我很有效
"plugins": [
"@babel/plugin-proposal-class-properties"
]
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
[
"@babel/plugin-proposal-class-properties"
]
]
}
用上述代码替换.babelrc文件。它为我解决了这个问题。webpack项目的解决方案
我只是通过将@babel/plugin提案类属性添加到webpack-config-plugin中来解决这个问题。
我的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']}]
}
}
]
}
在对同一个错误进行了近3个小时的搜索并花费时间后,我发现我正在使用名称导入进行React:
import { React } from 'react';
这是完全错误的。只需切换到:
import React from 'react';
所有的错误都消失了。
我希望这对某人有帮助。这是我的。巴别塔LRC:
{
"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"
}
}
- 安装插件建议类属性
npm安装@babel/plugin提案类属性——保存开发
- 通过添加以下内容更新您的webpack.config.js
'plugins':['@babel/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本机应用程序上对我有效,我认为这也将有助于React应用程序。我正在使用Thread。为了克服这个错误,我必须做以下几点
yarn add @babel/plugin-proposal-class-properties --dev
首先将:@babel/plugin建议类属性安装为开发人员依赖项:
npm install @babel/plugin-proposal-class-properties --save-dev
然后编辑您的.babelrc,使其精确如下:
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
[
"@babel/plugin-proposal-class-properties"
]
]
}
{
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'],
.
.
.
.babelrc文件位于根目录中,package.json位于根目录中
请注意,您应该重新启动webpack dev服务器以使更改生效。我正在显式使用babel解析器。上述解决方案对我都不起作用。这起作用了
const ast = parser.parse(inputCode, {
sourceType: 'module',
plugins: [
'jsx',
'classProperties', // '@babel/plugin-proposal-class-properties',
],
});
在构造函数中移动状态
对我来说很有效:
。。。
类MyComponent扩展组件{
建造师(男){
超级(男)
this.state={}
}
}
...
祝你好运…如果有人在monorepo上工作,那么你需要配置覆盖packages/web
中的.js
文件。您需要在该文件中添加resolveApp('../../node\u 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
};
添加
"plugins": [
[
"@babel/plugin-proposal-class-properties"
]
]
进入。babelrc
对我很有用 根据GitHub的问题,如果你使用create react应用程序,你应该将你的.babelrc
或babel.config.js
配置复制到webpack.config.js
并删除它们。因为有两行代码babelrc:false,configFile:false,
你在babelrc中的配置,。。这些都是无用的。
您的webpack.config.js
位于/node\u madules/react scripts/config
文件夹中
我这样解决了我的问题:
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
[
"@babel/plugin-proposal-class-properties"
]
]
}
{
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'],
.
.
.
在我的工作环境根目录中,.babelrc文件不存在。然而,package.json中的以下条目解决了这个问题
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]}
注意:在执行npm或warn命令之前,不要忘记退出控制台并重新打开。我为src/components->../../components创建了一个符号链接,导致npm start
发疯并停止将src/components/*.js解释为jsx,从而给出了相同的错误。所有从官方得到的修复指示都是无用的。
当我复制回组件目录时,一切都恢复正常 纱线添加--dev@babel/plugin提案类属性
或
npm安装@babel/plugin提案类属性——保存开发
.babelrc我在尝试用babel传输一些jsx时遇到了同样的问题。下面是对我有效的解决方案。您可以将以下json添加到.babelrc
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
{
"presets": [
[
"@babel/preset-react",
{ "targets": { "browsers": ["last 3 versions", "safari >= 6"] } }
]
],
"plugins": [["@babel/plugin-proposal-class-properties"]]
}
对于带有网页包的react项目:
Do:npm安装@babel/插件建议类属性——保存开发
在存在package.json
和webpack.config.js
的根文件夹中创建.babelrc
(如果不存在)文件,并添加以下代码:
{
“预设”:[“@babel/preset env”,“@babel/preset react”],
“插件”:[
[
“@babel/插件建议类属性”,
{
“松散”:真实
}
]
]
}
用于弹出的create react应用程序项目
我刚刚解决了我的问题,在我的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 */
],
您必须安装
npm install@babel/core@babel/plugin提案类properties@babel/preset env@babel/preset react babel loader
及
更改输入和输出
使用react state有两种方法:
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
npm run dev
or
npm run watch
选项1:
只需添加到package.json:
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
npm run dev
or
npm run watch
选项2:
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
npm run dev
or
npm run watch
1。在根文件夹中创建名为.babelrc的文件。
写入。babelrc:
{ "plugins": ["@babel/plugin-proposal-class-properties"] }
跑步:
npm i@babel/插件提案类属性
3。跑步:
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
npm run dev
or
npm run watch
确保您没有错误地从“react router dom/modules/BrowserRouter”导入import BrowserRouter代码>而不是从“react router dom”导入{BrowserRouter}
您不应该同时拥有/需要@babel/plugin提案类属性
和babel plug