我一直遵循这个tutorial,并且终生无法弄清为什么webpack无法识别JSX?希望有人指出我想念的愚蠢的事情:)
我的完整代码在这里 https://github.com/dukarc/django-react-app
错误:
\django-react-app\leadmanager\frontend\src\components\App.js: Unexpected token (6:11)
4 | class App extends Component {
5 | render() {
> 6 | return <h1>React App</h1>;
| ^
7 | }
8 | }
我尝试过的事情
版本(如果需要,将输出更多……这简直就是我的脑袋):
package.json
{
"name": "lead_manager_react_django",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "webpack --mode development ./leadmanager/frontend/src/index.js --output ./leadmanager/frontend/static/frontend/main.js",
"build": "webpack --mode production ./leadmanager/frontend/src/index.js --output ./leadmanager/frontend/static/frontend/main.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.3.1",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.5",
"babel-plugin-transform-class-properties": "^6.24.1",
"webpack": "^4.29.0",
"webpack-cli": "^3.2.1"
},
"dependencies": {
"prop-types": "^15.6.2",
"react": "^16.7.0",
"react-dom": "^16.7.0"
}
}
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
}
};
.babelrc
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": ["transform-class-properties"]
}
答案 0 :(得分:3)
我签出了您的源代码,它有一个小的错字,是getElementById
而不是getElementByID
。
以下文件中的第10行
https://github.com/dukarc/django-react-app/blob/master/leadmanager/frontend/src/components/App.js
它对我来说更正了。