当我在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": "", …Run Code Online (Sandbox Code Playgroud) 我安装了 npm install --save-dev @babel/preset-react,npm install --save-dev @babel/plugin-syntax-jsx并且npm install --save-dev @babel/plugin-proposal-class-properties。我在根文件夹中创建一个 .babelrc.json 文件,并将以下内容粘贴到其中:
{
"presets": ["@babel/preset-react"]
}
Run Code Online (Sandbox Code Playgroud)
之后我npm start再次跑步。但我有同样的错误。我什至将 .babelrc 放在我的 src 文件夹中,但我有同样的错误。我读过类似的错误帖子如何解决错误:“jsx”当前未启用,但我无法解决我的问题。我是reactjs新手,在我插入的链接中被写入运行npm run dev。但是当我在终端中运行此代码时,出现此错误“npm ERR!缺少脚本:“dev””并且我的项目中没有 webpack.config.js 文件。有必要吗?
我的 package.json 是
{
"name": "mysite",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.2.0",
"@testing-library/user-event": "^13.5.0",
"bootstrap": "^5.1.3",
"font-awesome": "^4.7.0",
"formik": "^2.2.9",
"json-server": "^0.4.2",
"mixitup": "^3.3.1",
"react": "^18.1.0",
"react-animated-cursor": "^2.4.0",
"react-bootstrap": "^2.4.0",
"react-dom": "^18.1.0",
"react-fancybox": "^1.0.2",
"react-owl-carousel": "^2.3.3", …Run Code Online (Sandbox Code Playgroud)