1Su*_*Sun 70 babel reactjs webpack babeljs
当我在Django项目中设置React时,我遇到了这个错误
模块构建中的ModuleBuildError失败(来自./node_modules/babel-loader/lib/index.js):SyntaxError:C:\ Users\1Sun\Cebula3\cebula_react\assets\js\index.js:支持实验语法'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.
Run Code Online (Sandbox Code Playgroud)
所以,我安装了@ babel/plugin-proposal-class-properties并把它放在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"
}
}
Run Code Online (Sandbox Code Playgroud)
babelrc
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
Run Code Online (Sandbox Code Playgroud)
但是错误仍然存在,有什么问题?
小智 58
更改
"plugins": [
"@babel/plugin-proposal-class-properties"
]
Run Code Online (Sandbox Code Playgroud)
至
"plugins": [
[
"@babel/plugin-proposal-class-properties",
{
"loose": true
}
]
]
Run Code Online (Sandbox Code Playgroud)
这对我有用
Fan*_*ing 34
我只是通过添加@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']}]
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
Awn*_*Ali 31
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
[
"@babel/plugin-proposal-class-properties"
]
]
}
Run Code Online (Sandbox Code Playgroud)
用上面的代码替换你的.babelrc文件.它解决了我的问题.
Raz*_*Raz 22
首先安装:@ babel / plugin-proposal-class-properties作为dev依赖项:
npm install @babel/plugin-proposal-class-properties --save-dev
Run Code Online (Sandbox Code Playgroud)
然后编辑您的.babelrc文件,使其完全像这样:
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
[
"@babel/plugin-proposal-class-properties"
]
]
}
Run Code Online (Sandbox Code Playgroud)
.babelrc文件位于根目录中,即package.json所在的位置。
请注意,您应该重新启动webpack开发服务器,以使更改生效。
Sah*_*ana 19
在我的工作环境根目录中,.babelrc 文件不存在。但是,在 package.json 中的以下条目解决了这个问题。
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]}
Run Code Online (Sandbox Code Playgroud)
注意:在执行 npm 或 yarn 命令之前,不要忘记退出控制台并重新打开。
有两种方法可以处理反应状态:
选项 1: 只需添加到 package.json:
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
Run Code Online (Sandbox Code Playgroud)
选项 2:
1.在根文件夹中创建一个名为.babelrc的文件。
在 .babelrc 中写入:
{ "plugins": ["@babel/plugin-proposal-class-properties"] }
Run Code Online (Sandbox Code Playgroud)
npm i @babel/plugin-proposal-class-properties
3. 运行:
npm run dev
or
npm run watch
Run Code Online (Sandbox Code Playgroud)
经过近 3 个小时的搜索并在同一错误上花费时间,我发现我正在为 React 使用名称导入:
import { React } from 'react';
Run Code Online (Sandbox Code Playgroud)
这是完全错误的。只需将其切换为:
import React from 'react';
Run Code Online (Sandbox Code Playgroud)
所有的错误都消失了。我希望这可以帮助别人。这是我的 .babelrc:
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
Run Code Online (Sandbox Code Playgroud)
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'
})
]
}
Run Code Online (Sandbox Code Playgroud)
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"
}
}
Run Code Online (Sandbox Code Playgroud)
我发现我的问题.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',
],
};
Run Code Online (Sandbox Code Playgroud)
它适用于 React Native 应用程序,我认为这也有助于 React 应用程序。
移动state内部constructor function对我有用:
...
class MyComponent extends Component {
constructor(man) {
super(man)
this.state = {}
}
}
...
Run Code Online (Sandbox Code Playgroud)
祝你好运...
小智 6
安装plugin-proposal-class-properties
npm install @babel/plugin-proposal-class-properties --save-dev
通过添加更新您的webpack.config.js
'plugins': ['@babel/plugin-proposal-class-properties']}]
根据这个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'],
.
.
.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
47266 次 |
| 最近记录: |