Webpack not serving js files to browser UPDATE: website runs in Safari but not Chrome?

huangapple go评论76阅读模式
英文:

Webpack not serving js files to browser UPDATE: website runs in Safari but not Chrome?

问题

以下是翻译好的部分:

  • 在浏览器中,bundle.js 文件包含 index.html 代码,而在 Node.js 中包含正确的 JavaScript 代码。有任何想法为什么会这样?注意:以前我的项目的旧版本仍然有效,而且 webpack 配置完全相同。

期望行为:

Webpack not serving js files to browser UPDATE: website runs in Safari but not Chrome?

实际行为:
Webpack not serving js files to browser UPDATE: website runs in Safari but not Chrome?

  • 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      

Webpack not serving js files to browser UPDATE: website runs in Safari but not Chrome?

  • Node.js 中的 bundle.js
    Webpack not serving js files to browser UPDATE: website runs in Safari but not Chrome?

更新:网站在 Safari 中运行,但在 Chrome 中不运行??

  • Webpack not serving js files to browser UPDATE: website runs in Safari but not 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 not serving js files to browser UPDATE: website runs in Safari but not Chrome?

ACTUAL behavior:
Webpack not serving js files to browser UPDATE: website runs in Safari but not Chrome?

  • 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      

Webpack not serving js files to browser UPDATE: website runs in Safari but not Chrome?

  • bundle.js in node
    Webpack not serving js files to browser UPDATE: website runs in Safari but not Chrome?

UPDATE: website runs in Safari but not Chrome???

  • Webpack not serving js files to browser 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.

huangapple
  • 本文由 发表于 2023年2月10日 12:58:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定