javascript Webpack:找不到 bundle.js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29923277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:17:56  来源:igfitidea点击:

Webpack: can't find bundle.js

javascriptnode.jswebpack

提问by yccteam

I've finally got the he dev server running and I get something on screen. I've setup a "start" script for NPM like this:

我终于让他的开发服务器运行了,我在屏幕上看到了一些东西。我已经为 NPM 设置了一个“启动”脚本,如下所示:

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

I get an error:

我收到一个错误:

http://localhost:8080/bundle.js Failed to load resource: the server responded with a status of 404 (Not Found)

My folders are set as follows:

我的文件夹设置如下:

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

My webpack.config.js:

我的 webpack.config.js:

module.exports = {
    context: __dirname + '/app',
    entry: './index.js',
    output: {
        path: __dirname + '/app',
        filename: './bundle.js'
    }
}

Can you tell what's wrong?

你能说出什么问题吗?

回答by Lim H.

bundle.js is located inside your /appdirectory. That pathoption in output specifies the absolute path that the file goes.

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

Also you don't need the ./in filename. It will get resolved relatively to output.pathbut it is confusing and may have contributed to your problem.

你也不需要./in 文件名。它将得到相对解决,output.path但它令人困惑并且可能导致您的问题。

回答by Hemadri Dasari

The problem mostly with pointing bundle js in index.html. The reason webpack bundle.js is not found because you need to specify absolute path in index.html. Say suppose your bundle.js and index.html is generated under dist folder, then it should be something like below.

问题主要是在 index.html 中指向 bundle js。找不到webpack bundle.js的原因是需要在index.html中指定绝对路径。假设你的 bundle.js 和 index.html 是在 dist 文件夹下生成的,那么它应该像下面这样。

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