我运行服务器localhost并收到错误 bundle.js:1未捕获的SyntaxError:意外的令牌< 在输出bundle.js是index.html文件的html代码。这是设置我的webpack.config文件。你能告诉我设置有什么问题吗?
import path from 'path';
import webpack from 'webpack';
export default {
devtool: 'eval-source-map',
entry: [
'webpack-hot-middleware/client',
path.join(__dirname, '/client/index.js' )
],
output: {
path: '/',
publicPath: '/'
},
plugins: [
new webpack.NoEmitOnErrorsPlugin(),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin()
],
module: {
loaders: [
{
test: /\.js$/,
include: path.join(__dirname, 'client'),
loaders: ['react-hot-loader', 'babel-loader' ]
}
]
},
resolve: {
extensions: ['.js']
}
}
的index.html
<html>
<head>
<meta charset="UTF-8">
<title>Site</title>
<meta content="width=device-width, initial-scale=1" name="viewport" />
</head>
<body>
<h1>Hello bla bla bla</h1>
<div id="app"></div>
<script src="bundle.js"></script>
</body>
</html>
服务器/ index.js
import express from 'express';
import path from 'path';
import webpack from 'webpack';
import webpackMiddeleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import webpackConfig from '../webpack.config';
let app = express();
const compiler = webpack(webpackConfig);
app.use(webpackMiddeleware(compiler, {
hot: true,
publicPath: webpackConfig.output.publicPath,
noInfo: true
}));
app.use(webpackHotMiddleware(compiler));
app.get('/*', (req, res)=>{
res.sendFile(path.join(__dirname, './index.html'));
});
app.listen('3000', ()=>{console.log('Running on port 3000')});
的客户机/ index.js
import React from 'react';
import { render } from 'react-dom';
import App from './components/App';
render(<App/>, document.getElementById('app'));
组件/ App.js
import React from 'react';
class App extends React.Component {
render(){
return (
<h1>Hello</h1>
);
}
}
export default App;
的package.json:
{
"name": "xxxxx",
"version": "1.0.0",
"description": "xxxxxxxxxx",
"main": "index.js",
"scripts": {
"server": "nodemon --watch server --exec babel-node -- server/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "xxxxxxxxxxxxx"
},
"keywords": [
"
],
"author": "xxxxxxxxx",
"license": "ISC",
"bugs": {
"url": "xxxxxxxxxxxxxxxxxxxxxxx"
},
"homepage": "xxxxxxxxxxxxxxxxxxxx",
"dependencies": {
"babel-cli": "^6.24.1",
"express": "^4.15.3",
"react": "^15.5.4",
"react-dom": "^15.5.4"
},
"devDependencies": {
"babel-loader": "^7.0.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"nodemon": "^1.11.0",
"react-hot-loader": "^1.3.1",
"webpack": "^2.6.1",
"webpack-dev-middleware": "^1.10.2",
"webpack-hot-middleware": "^2.18.0"
}
}
.babelrc
{
"presets": [ "es2015", "react" ]
}
谢谢。
答案 0 :(得分:8)
bundle.js
标记中的script
src错误。它将返回您的主index.html
,这就是您收到该错误的原因 - JS解析器正在尝试解析HTML文件。
您必须在脚本src中添加斜杠:
<script src="/bundle.js"></script>
如果这不起作用,您必须仔细检查output
配置。
答案 1 :(得分:0)
您正在使用多个入口点但未设置输出。
答案 2 :(得分:0)
我最终将显式路径放置到index.html中的bundle.js文件中:
来自:
<script src="bundle.js"></script>
至:
<script src="public/bundle.js"></script>
答案 3 :(得分:0)
Webpack 5.46.0 版中针对此问题的一个可能修复。节点 14.x。
我发布此信息是希望帮助其他人,如果他们到达此地并且仍未找到解决方法。因此,与许多其他问题一起,一种可能性是,如果您使用 html-webpack-plugin
,其中需要在属性中显式设置 publicPath
属性,而以前的 webpack < 5 版本的情况并非如此插件,据说它从 output/devServer 继承了属性。将属性添加到插件后,它可以在 webpack 5 中使用最新的 html-webpack-plugin。
这是相关的配置。
module.exports = {
mode: 'development',
entry: ['@babel/polyfill', './config/polyfills', './src/index.js'],
output: {
hotUpdateMainFilename: '[id].hot-update.[fullhash].json',
hotUpdateChunkFilename: '[id].hot-update.[fullhash].js',
path: resolve(__dirname, '..', 'dist'),
filename: '[name].[fullhash].js',
},
optimization: {
moduleIds: 'named',
},
devtool: 'eval-cheap-module-source-map',
plugins: [
new webpack.HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({
hash: true,
publicPath: '/yourPath',
template: resolve(__dirname, '..', 'src', 'index.html'),
alwaysWriteToDisk: true,
}),
new HtmlWebpackHarddiskPlugin({
outputPath: resolve(__dirname, '..', 'dist'),
}),
],
};
答案 4 :(得分:-1)
我通过向对象输出添加filename
项来解决了这个问题:
output: {
path: '/',
filename: 'bundle.js'
},