英文:
Webpack not serving js files to browser UPDATE: website runs in Safari but not Chrome?
问题
以下是翻译好的部分:
- 在浏览器中,bundle.js 文件包含 index.html 代码,而在 Node.js 中包含正确的 JavaScript 代码。有任何想法为什么会这样?注意:以前我的项目的旧版本仍然有效,而且 webpack 配置完全相同。
期望行为:
实际行为:
- webpack.config.js
const path = require('path');
const Dotenv = require('dotenv-webpack');
const clientConfig = {
mode: 'development',
entry: {
path: path.join(__dirname, './client/src/index.js')
},
output: {
path: path.join(__dirname, './client/dist'),
filename: 'bundle.js'
},
devServer: {
historyApiFallback: true
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /nodeModules/,
loader: 'babel-loader'
},
{
test: /\.(css|scss)/,
use: ['style-loader', 'css-loader', 'sass-loader']
},
{
test: /\.(png|pdf)/,
loader: 'file-loader'
}
]
},
plugins: [
new Dotenv({systemvars: true})
]
}
module.exports = clientConfig;
- 文件结构(所有相关文件)
-- 根目录
| \
| -- 客户端
| \
| -- dist
| | \
| | -- index.html
| | -- scss
| | \
| | -- index.scss
| \ ...
| -- src
| \
| -- index.js
| -- components
| \
| -- App.js
| ...
-- webpack.config.js
- Node.js 中的 bundle.js
更新:网站在 Safari 中运行,但在 Chrome 中不运行??
英文:
The bundle.js file in the browser contains the index.html code, while in node it has the correct javascript code. Any idea why this is? NOTE: this wasn't always the case, older versions of my project still workand the webpack configuration is exactly the same.
EXPECTED behavior:
- webpack.config.js
const path = require('path');
const Dotenv = require('dotenv-webpack');
const clientConfig = {
mode: 'development',
entry: {
path: path.join(__dirname, './client/src/index.js')
},
output: {
path: path.join(__dirname, './client/dist'),
filename: 'bundle.js'
},
devServer: {
historyApiFallback: true
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /nodeModules/,
loader: 'babel-loader'
},
{
test: /\.(css|scss)$/,
use: ['style-loader', 'css-loader', 'sass-loader']
},
{
test: /\.(png|pdf)$/,
loader: 'file-loader'
}
]
},
plugins: [
new Dotenv({systemvars: true})
]
}
module.exports = clientConfig;
- file structure (all relevant files)
-- root
| \
| -- client
| \
| -- dist
| | \
| | -- index.html
| | -- scss
| | \
| | -- index.scss
| \ ...
| -- src
| \
| -- index.js
| -- components
| \
| -- App.js
| ...
-- webpack.config.js
UPDATE: website runs in Safari but not Chrome???
答案1
得分: 0
None of my projects were running on the chrome browser. Whenever I had to run something with webpack I would receive this error message
Webpack error bundle.js:1 Uncaught SyntaxError: Unexpected token <
However, my projects would work just fine in the Safari browser. After a quick Google search I stumbled across this article https://kinsta.com/knowledgebase/this-site-cant-be-reached/#:~:text=Clear%20Your%20Browser%20Cache&text=To%20solve%20that%20issue%2C%20you,to%20clear%20your%20browser%20cache.&text=Clearing%20cached%20images%20and%20files,was%20giving%20you%20problems%20earlier.
I decided to wing it and I cleared my browser's cache of all data and cookies from the past 24 hours.
The error is gone and my sites work now.
I'm not entirely sure what happened but the issue is resolved. If anyone wants to contribute or elaborate fell free to do so.
英文:
None of my projects were running on the chrome browser. Whenever I had to run something with webpack I would recieve this error message
Webpack error bundle.js:1 Uncaught SyntaxError: Unexpected token <
However, my projects would work just fine in the Safari browser.
After a quick Google search I stumbled across this article
https://kinsta.com/knowledgebase/this-site-cant-be-reached/#:~:text=Clear%20Your%20Browser%20Cache&text=To%20solve%20that%20issue%2C%20you,to%20clear%20your%20browser%20cache.&text=Clearing%20cached%20images%20and%20files,was%20giving%20you%20problems%20earlier.
I decided to wing it and I cleared my browser's cache of all data and cookies from the past 24 hours.
The error is gone and my sites work now.
I'm not entirely sure what happened but the issue is resolved. If anyone wants to contribute or elaborate fell free to do so.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论