当前未启用对实验性语法"classProperties"的支持。

174

在我将React集成到Django项目中时,遇到了以下错误:

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.

因此,我安装了@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"
  }
}

babelrc

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

然而错误仍然存在,问题出在哪里?

你不应该同时使用 @babel/plugin-proposal-class-propertiesbabel-plugin-transform-class-properties。你安装后重新构建了吗? - SamVK
你正在运行哪个版本的Babel? - SamVK
分享你的 package.json 文件。 - Sakhi Mansoor
我编辑了我的 package json。 - 1Sun
尝试运行 npx babel-upgrade --write --install - FDisk
@FDisk,这个不起作用。 - secondman
23个回答

0

请确保您没有错误地导入了 import BrowserRouter from "react-router-dom/modules/BrowserRouter"; 而不是 import {BrowserRouter} from "react-router-dom";


0

我为src/components创建了一个符号链接 -> ../../components,导致npm start出现问题并停止将src/components/*.js解释为jsx,从而产生相同的错误。所有官方babel修复指南都无用。当我将components目录复制回来时,一切恢复正常!


1
你找到解决方案了吗? 我有一个应用程序,依赖项以正常方式安装,我能够测试它, 但是当我使用来自yarn link的库时,它会抛出上述错误。 - mdsadiq
1
@mdsadiq 抱歉,我还没有测试最新版本,而且这个问题仍然存在:https://github.com/facebook/create-react-app/issues/9040 也许你可以点赞,谢谢。 - martin
一种解决方案,允许react-scripts与符号链接一起工作:https://github.com/facebook/create-react-app/pull/7993/files - CefBoud

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接