Babel与Webpack构建报错求助:已配置@babel/preset-react仍异常
React + Webpack + Babel 构建报错求助
执行webpack build命令时出现语法错误,提示Unexpected token '<',说明JSX语法未被正确转译。已尝试在.babelrc和babel.config.json中配置@babel/preset-react,但问题依旧。附上项目所有配置文件,求解决思路。
报错详情
构建过程中抛出Unexpected token '<'错误,报错位置指向代码中的JSX标签,表明Babel未对JSX进行转译处理。
package.json 配置
{ "name": "@mahiraltinkaya/react-image-upload", "version": "1.2.1", "description": "", "main": "dist/index.js", "scripts": { "build": "webpack --mode production" }, "repository": { "type": "git", "url": "git+ssh://git@github.com/mahiraltinkaya/react-file-upload.git" }, "keywords": [ "react", "next", "react-image", "image-drag-n-drop" ], "author": "Mahir Altinkaya", "license": "ISC", "bugs": { "url": "https://github.com/mahiraltinkaya/react-file-upload/issues" }, "homepage": "https://github.com/mahiraltinkaya/react-file-upload#readme", "devDependencies": { "@babel/cli": "^7.20.7", "@babel/core": "^7.20.12", "@babel/preset-env": "^7.20.2", "@babel/preset-react": "^7.18.6", "babel-loader": "^9.1.2", "css-loader": "^6.7.3", "style-loader": "^3.3.1", "webpack": "^5.75.0", "webpack-cli": "^5.0.1" }, "dependencies": { "prop-types": "^15.8.1", "react": "^16.14.0" } }
babel.config.json 配置
{ "presets": ["@babel/preset-env", "@babel/preset-react"] }
webpack.config.json 配置
var path = require("path"); module.exports = { mode: "production", entry: "./src/index.js", output: { path: path.resolve("dist"), filename: "index.js", libraryTarget: "commonjs2", }, module: { rules: [ { test: /\.js$/, exclude: /node_modules/, loader: "babel-loader", }, { test: /\.css$/, loader: "css-loader", }, ], }, externals: { react: "react", }, };
内容的提问来源于stack exchange,提问作者Mahir Altınkaya
相关产品推荐
相关产品推荐

