我正在尝试为React Web应用程序设置样板。
但是,每次导航到http://localhost:8080/时,都会出现一条错误消息:Uncaught SyntaxError:Unexpected token <
。在我的bundle.js中。
我不知道该怎么办!谢谢你的帮助。
这是我的一些文件:
/public/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>New app</title>
<link rel="stylesheet" href="/style.css" />
<script src="./bundle.js" defer> .</script>
</head>
<body>
<div id="app"></div>
</body>
</html>
/client/app.js
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(
<div>Hello, world!</div>,
document.getElementById('app')
);
webpack.config.js
module.exports = {
entry: [
'@babel/polyfill', // enables async-await
'./client/app.js',
],
mode: 'development',
output: {
path: __dirname,
filename: `./public/bundle.js`,
},
devtool: 'source-maps',
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
],
},
};
package.json
{
"name": "boilerplatelivmarx",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server",
"start-dev": "webpack -w & nodemon server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel-preset-env": "^1.7.0",
"babel-preset-stage-2": "^6.24.1",
"express": "^4.16.4",
"path": "^0.12.7",
"react": "^16.6.0",
"react-dom": "^16.6.0",
"react-router-dom": "^4.3.1"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-preset-react": "^6.24.1",
"webpack": "^4.23.1",
"webpack-cli": "^3.1.2"
}
}
谢谢!
答案 0 :(得分:0)
您的问题是错误的jsx
processing;
将反应预设添加到您的webpack.config.js
:
...
use: {
loader: 'babel-loader',
options: {
presets: ['react']
}
},
...
此外,您似乎尝试使用@babel/polyfill
,但没有安装它。
运行:
npm i @babel/polyfill
您在<script>
文件中的index.html
中不需要点:
<!-- ↓ -->
<script src="./bundle.js" defer> .</script>
在那之后,一切都必须建立:
然后运行: