Webpack: 找不到bundle.js文件

7

我终于成功启动了开发服务器,并在屏幕上看到了一些东西。 我已经像这样为 NPM 设置了一个“start”脚本:

"start": "webpack-dev-server --content-base app"

我遇到了一个错误:
http://localhost:8080/bundle.js Failed to load resource: the server responded with a status of 404 (Not Found)

我的文件夹设置如下:

appDir
  ->app
  ->node_modules
  webpack.config.js
  package.json

我的webpack.config.js文件:
module.exports = {
    context: __dirname + '/app',
    entry: './index.js',
    output: {
        path: __dirname + '/app',
        filename: './bundle.js'
    }
}

你能说出哪里有问题吗?

2个回答

9

bundle.js位于您的/app目录中。output中的path选项指定文件的绝对路径。

此外,您无需在文件名中添加./。它将相对于output.path进行解析,但这会导致混淆并可能导致问题。


1
问题主要是在于在index.html中指定bundle js的路径。webpack bundle.js找不到的原因是你需要在index.html中指定绝对路径。比如说,假设你的bundle.js和index.html都是在dist文件夹下生成的,那么应该像下面这样写:
<script src="/bundle.js"></script>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接