【问题标题】:Bundle.js and Css file are not getting load with webpackBundle.js 和 Css 文件没有被 webpack 加载
【发布时间】:2020-05-09 09:30:33
【问题描述】:

我最近学习了 react 和 webpack。我在制作项目时遇到了问题。

当我打开 index.html.In 浏览器网络时,它显示 bundle.js not found..而且我也看不到 CSS 文件。请帮帮我。

我不知道为什么我的文件没有显示在网络中。 我的所有文件都成功打包了。

请帮我看看我的代码哪里错了。

我的文件夹结构

node modules
app-(contains js jsx and css files)
public- (contains single bundle file.There is no bundle.js.map file)
package.json
package-lock.json
webpack.config.js
index.html

我的 webpack 配置

var path=require("path");

module.exports={
    entry:path.resolve(__dirname,"app"),

    output:{
    path:path.resolve(__dirname,"public"),
    filename:" bundle.js",
    },

    module:{
        rules:[
            {
                test:/\.jsx?$/,
                include:path.resolve(__dirname,"app"),
                use:[{loader:'babel-loader',

                     options: {
                                 presets: ['@babel/preset-react',{
                          'plugins': ['@babel/plugin-proposal-class-properties']}]
                             }
                     }]
            },
            {
                test:/\.css$/,
                include:path.resolve(__dirname,"app"),
                use:["style-loader","css-loader"]
            }

        ]

    }

};

我的包.json

{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack -p"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@babel/plugin-proposal-class-properties": "^7.8.3",
    "@babel/preset-env": "^7.9.6",
    "babel-loader": "^8.1.0",
    "css-loader": "^3.5.3",
    "express": "^4.17.1",
    "nodeman": "^1.1.2",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-redux": "^7.2.0",
    "redux": "^4.0.5",
    "redux-actions": "^2.6.5",
    "style-loader": "^1.2.1",
    "webpack": "^4.43.0",
    "webpack-dev-server": "^3.11.0"
  },
  "devDependencies": {
    "@babel/core": "^7.9.6",
    "@babel/preset-react": "^7.9.4",
    "webpack-cli": "^3.3.11"
  }
}


index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link href="https://fonts.googleapis.com/css2?family=Merriweather&display=swap" rel="stylesheet">

    <title>React</title>
  </head>
  <body>
    <div id="root"></div>

  </body>
  <script type="text/javascript" src="public/bundle.js"></script>
</html>

【问题讨论】:

  • public 文件夹中有bundle.js 文件吗?如果没有,您需要运行构建。
  • 是的,我已经公开了。它已成功构建但未显示文件(未找到 bundle.js)
  • 因为在定义捆绑包的文件名时引号位于错误的位置,并且字符串中有空格:filename:" bundle.js"。您正在创建带有前导空格的 bundle.js。

标签: reactjs webpack babeljs


【解决方案1】:

将 bundle.js 的路径更改为 index.html 文件中的 src="/public/bundle.js" 可能会解决问题。

说明:

script标签指定bundle.js文件的位置为

src="public/bundle.js"

此路径没有前导“/”。因此,路径将被解释为相对于浏览器正在读取的当前文件的位置。

在路径中添加前导 / 将使浏览器从相对于文档根目录的路径请求脚本文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 2021-11-01
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    相关资源
    最近更新 更多