错误:加载块0失败。 – webpack试图加载0.js

我有以下项目结构:

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

库文件使用requirejs define([], function() {})语法。

所以我把它放到了webpack.config.js中:

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

然后我在webpack的入口点mainFile.js中做这个:

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

我得到这个错误:

 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. 

所以webpack试图加载一些0.js文件。 那应该是什么?

AMD requireasynchronous加载模块,并像require.ensure一样require.ensure 。 Webpack会拆分这些dynamic导入并在使用时请求它们。 如果你不给他们一个名字,他们将被编号( 0.js等或你在output.chunkFilenameconfiguration的)。

如果您不需要拆分代码,则需要定期导入它们,build议使用ES模块 。

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

有关代码拆分的更多信息,请参阅指南 – 代码拆分和build议的import()函数。