简体   繁体   English

错误:加载块0失败。 - webpack尝试加载0.js

[英]Error: Loading chunk 0 failed. - webpack tries to load 0.js

I have following project structure: 我有以下项目结构:

|-mainFile.js
|-scripts -
          |-Library1.js
          |-Library2.js

Library files use requirejs define([], function() {}) syntax. 库文件使用requirejs define([], function() {})语法。

I've therefore put this into the webpack.config.js: 因此我把它放到webpack.config.js中:

module.exports = {
    resolve: {
        modules: [
            path.resolve(__dirname, 'scripts'),
            // The normal path
            "node_modules"
        ]
    },
    /** ... **/
}

I then do this in mainFile.js which is entry point for webpack: 然后我在mainFile.js中执行此操作,这是webpack的入口点:

require(["Library1", "Library2"], function(Library1, Library2) { ... });

And I get this error: 我收到这个错误:

GET http://localhost:3000/0.js [HTTP/1.1 404 Not Found 3ms]
Error: Loading chunk 0 failed.
Stack trace:
onScriptComplete@http://localhost:3000/player/webpack_build.js:100:24
                     webpack_build.js:146:52
The resource from “http://localhost:3000/0.js” was blocked due to MIME type mismatch (X-Content-Type-Options: nosniff).[Learn More]
test_file.html Error: Loading chunk 0 failed.

So webpack tries to load some 0.js file. 所以webpack尝试加载一些0.js文件。 What's that supposed to be? 应该是什么?

I have figured out a fix, I hope it helps. 我想出了一个修复,我希望它有所帮助。

For my React Routes, I'm using dynamic loading with import statements, without the CommonChunks plugin. 对于我的React Routes,我使用带有import语句的动态加载,没有CommonChunks插件。

I was getting the same error (or "Chunk 1", "Chunk 2", etc) depending on the route I was loading. 我得到了相同的错误(或“块1”,“块2”等),具体取决于我加载的路线。 I finally realized that my asset bundles were actually being outputted into the current directory that my html was in, even though my output.path was pointing to the assets folder. 我终于意识到我的资产包实际上被输出到我的html所在的当前目录中,即使我的output.path指向assets文件夹。

To fix this, I just set my output.chunkFilename to '../../../assets/js/com/[name].bundle.js', which then output it to the correct folder. 要解决这个问题,我只需将output.chunkFilename设置为'../../../assets/js/com/[name].bundle.js',然后将其输出到正确的文件夹。

From then on, my app was able to find my bundles and it worked great! 从那时起,我的应用程序能够找到我的捆绑,它工作得很好!

Hope it helps, Michael 希望它有所帮助,迈克尔

The AMD require loads the modules asynchronously and works like require.ensure . AMD require异步加载模块并且像require.ensure一样require.ensure Webpack will split out these dynamic imports and request them when they are used. Webpack将拆分这些动态导入并在使用它们时请求它们。 If you don't give them a name they will be numbered ( 0.js etc. or what you configured in output.chunkFilename ). 如果你没有给他们一个名字,他们将被编号( 0.js等或你在output.chunkFilename配置的)。

In case you don't need to split the code, you import them regularly and ES modules are recommended. 如果您不需要拆分代码,可以定期导入它们,建议使用ES模块

import Library1 from "Library1"
import Library2 from "Library2"

For more information on code splitting see Guides - Code Splitting and the recommended import() function. 有关代码拆分的更多信息,请参阅指南 - 代码拆分和推荐的import()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM