reactjs - 未捕获的语法错误:意外的令牌 < main.b6d3c706.chunk.js:1
问题描述
索引.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Muli" />
<link rel="stylesheet" type="text/css" href="static/css/normalize.css" />
<link rel="stylesheet" type="text/css" href="static/css/demo.css" />
<link rel="stylesheet" type="text/css" href="static/css/component.css" />
<link rel="stylesheet" type="text/css" href="static/css/game.css" />
<!-- <link href="css/sb-admin-2.min.css" rel="stylesheet"> -->
<link href="static/css/graph-cards.css" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<body>
<div id="root"></div>
</body>
</html>
包.json
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"@amcharts/amcharts4": "^4.4.1",
"@amcharts/amcharts4-geodata": "^4.1.4",
"history": "^4.9.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-radio-buttons": "^1.2.2",
"react-router-dom": "^5.0.0",
"react-scripts": "2.1.8",
"tachyons": "^4.11.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
index.js
import React, { Suspense } from 'react';
import ReactDOM from "react-dom";
import 'tachyons';
import { createBrowserHistory } from "history";
import { Router, Route, Switch } from "react-router-dom";
import indexRoutes from "routes/index";
var hist = createBrowserHistory();
ReactDOM.render(
<Router history={hist}>
<Suspense fallback={<div>Loading...</div>}>
<Switch>
{indexRoutes.map((prop, key) => {
return <Route path={prop.path} key={key} component={prop.component} />;
})}
</Switch>
</Suspense>
</Router>,
document.getElementById("root")
);
在这里,我正在使用 react 的生产版本。我在控制台中遇到错误。
Uncaught SyntaxError: Unexpected token <
main.b6d3c706.chunk.js:1 Uncaught SyntaxError: Unexpected token <
我分享了我的 index.html、index.js 和 package.json
我需要更改 webpack 中的任何其他配置吗?我已将 src 标签更改为“text/babel”,但它仍然无法正常工作。
import React from 'react';
const Home = React.lazy(() => import('views/Home/Home'));
var indexRoutes = [
{ path: "/", name: "Home", component: Home }
];
export default indexRoutes;
请看一看。
解决方案
我和你有同样的问题我要做的就是这个
尝试在您的服务器上更改此设置
const root = require("path").join(__dirname, "../build");
app.use(express.static(root));
app.get("*", (req, res) => {
res.sendFile("index.html", { root });
});
推荐阅读
- android - 出了什么问题:任务':fluttertoast:compileDebugKotlin'执行失败
- cakephp - 无法实例化抽象类 Cake\Mailer\Mailer
- python-3.x - 如何每次在 Python 中的同一个变量上获得不同的随机数?
- php - 如何通过子数组值从数组中获取值?
- aframe - 带有lottie json的A帧
- python - Kafka 与 python:如何将主题发送到 postgreSQL?
- python - 为什么我无法在 python 中使用 selenium 显示产品价格?
- python - git filter-branch调用的python脚本下的git init使用错误的目录
- node.js - 如何关闭使用 fs.readFile/writeFile 打开的文件描述符
- python - 将数据框值重塑为 pandas/numpy 中的单个基数