Webpack:找不到 bundle.js

Webpack: can't find bundle.js

我终于得到了他的开发服务器 运行 并且我在屏幕上看到了一些东西。 我已经为 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'
    }
}

你能告诉我哪里出了问题吗?

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

此外,您不需要文件名中的 ./。它会相对于 output.path 得到解决,但它令人困惑并且可能导致了您的问题。

问题主要与指向 index.html 中的 bundle js 有关。之所以找不到webpack bundle.js,是因为需要在index.html中指定绝对路径。假设你的 bundle.js 和 index.html 是在 dist 文件夹下生成的,那么它应该如下所示。

<script src="/bundle.js"></script>