【问题标题】:bundle.js not found [Webpack]找不到 bundle.js [Webpack]
【发布时间】:2018-11-19 07:12:43
【问题描述】:

[UPDATE] bundle.js 实际上是在内存中创建的。最好的方法是将 index.html 和 bundle.js(在 webpack.config.js 中配置)保持在同一个目录中以避免任何问题。

我一直在尝试使用 webpack 渲染一个简单的 html 文件,但不知道为什么会出现 404。我知道找不到 bundle.js,所以我尝试了不同的路径,但没有成功,有什么想法吗?

感谢您的帮助。 谢谢。

app.js

var express = require('express')
var path = require('path')

const app = express();

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html')

app.get('/', function (req, res) {
    res.render('index')
  })

app.listen(3000, () => console.log('Example app listening on port 3000!'))

webpack.config.js

module.exports = {
  entry: ['./src/index.js'],
  output: {
    path: __dirname,
    publicPath: '/',
    filename: 'bundle.js'
  },
[...]

index.html

<!DOCTYPE html>
<html>
  [...]
  <body>
      <div class="container"></div>
  </body>
  <script src="./bundle.js"></script>
</html>

文件夹结构

【问题讨论】:

  • 如何编译“./src/index.js”?你需要使用 webpack bundler 来编译前端源码
  • webpack 将 index.js 作为入口,并将其放入 bundle.js 中,但我猜这不是问题,问题是渲染时找不到 bundle.js

标签: node.js reactjs webpack


【解决方案1】:

您没有为index 文件指定正确的路径。如果您将它放在src directory 上,代码将如下所示:

entry: [
    path.resolve(__dirname, 'src/index')
  ],
[...]

否则,如果您在 views directory, 他们上有它,代码将如下所示:

 entry: [
        path.resolve(__dirname, 'views/index')
      ],
    [...]

在你的html文件中是&lt;script src="/bundle.js"&gt;&lt;/script&gt;


更新

根据您在 github 上的代码尝试更改以下行

entry: [
    path.resolve(__dirname, 'src/index')
  ],
  devServer: {
    contentBase: path.resolve(__dirname, 'src')
  },
    output: {
    path: __dirname + '/dist', // Note: Physical files are only output by the production build task `npm run build`.
    publicPath: '/',
    filename: 'bundle.js'
  },

问题在于您在entry point 和devServer 中都缺少path.resolve(...)

希望有帮助:)

【讨论】:

  • 您好,感谢您的帮助,但不幸的是它对我不起作用,现在我收到了无法访问网站的错误
  • @MinhPham 更新了答案...这样可以解决您的问题吗?
  • 我仍然收到这些错误:GET localhost:3030/bundle.js 404 (Not Found) localhost/:1 Refused to execute script from 'localhost:3030/bundle.js' 因为它的 MIME 类型('text/html')不是可执行文件,并启用严格的 MIME 类型检查。
  • @MinhPham 很奇怪......你能提供你在 webpack.config.js 上的全部代码,以便我检查真正的问题在哪里吗?
  • @MinhPham 更新了答案。让我知道这是否能解决您的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-08
  • 2016-04-02
  • 1970-01-01
  • 2017-09-18
相关资源
最近更新 更多