故障处理

Webpack 的配置比较复杂,很容出现错误,下面是一些通常的故障处理手段。

一般情况下,webpack 如果出问题,会打印一些简单的错误信息,比如模块没有找到。我们还可以通过参数 --display-error-details 来打印错误详情。

  1. $ webpack --display-error-details
  2. Hash: a40fbc6d852c51fceadb
  3. Version: webpack 1.12.2
  4. Time: 586ms
  5. Asset Size Chunks Chunk Names
  6. bundle.js 12.1 kB 0 [emitted] main
  7. [0] ./entry.js 153 bytes {0} [built] [1 error]
  8. [5] ./module.js 43 bytes {0} [built]
  9. + 4 hidden modules
  10. ERROR in ./entry.js
  11. Module not found: Error: Cannot resolve 'file' or 'directory' ./badpathmodule in /Users/zhaoda/data/projects/webpack-handbook/examples
  12. resolve file
  13. /Users/zhaoda/data/projects/webpack-handbook/examples/badpathmodule doesn't exist
  14. /Users/zhaoda/data/projects/webpack-handbook/examples/badpathmodule.webpack.js doesn't exist
  15. /Users/zhaoda/data/projects/webpack-handbook/examples/badpathmodule.js doesn't exist
  16. /Users/zhaoda/data/projects/webpack-handbook/examples/badpathmodule.web.js doesn't exist
  17. /Users/zhaoda/data/projects/webpack-handbook/examples/badpathmodule.json doesn't exist
  18. resolve directory
  19. /Users/zhaoda/data/projects/webpack-handbook/examples/badpathmodule doesn't exist (directory default file)
  20. /Users/zhaoda/data/projects/webpack-handbook/examples/badpathmodule/package.json doesn't exist (directory description file)
  21. [/Users/zhaoda/data/projects/webpack-handbook/examples/badpathmodule]
  22. [/Users/zhaoda/data/projects/webpack-handbook/examples/badpathmodule.webpack.js]
  23. [/Users/zhaoda/data/projects/webpack-handbook/examples/badpathmodule.js]
  24. [/Users/zhaoda/data/projects/webpack-handbook/examples/badpathmodule.web.js]
  25. [/Users/zhaoda/data/projects/webpack-handbook/examples/badpathmodule.json]
  26. @ ./entry.js 3:0-26

Webpack 的配置提供了 resolveresolveLoader 参数来设置模块解析的处理细节,resolve 用来配置应用层的模块(要被打包的模块)解析,resolveLoader 用来配置 loader 模块的解析。

当引入通过 npm 安装的 node.js 模块时,可能出现找不到依赖的错误。Node.js 模块的依赖解析算法很简单,是通过查看模块的每一层父目录中的 node_modules 文件夹来查询依赖的。当出现 Node.js 模块依赖查找失败的时候,可以尝试设置 resolve.fallbackresolveLoader.fallback 来解决问题。

  1. module.exports = {
  2. resolve: { fallback: path.join(__dirname, "node_modules") },
  3. resolveLoader: { fallback: path.join(__dirname, "node_modules") }
  4. };

Webpack 中涉及路径配置最好使用绝对路径,建议通过 path.resolve(__dirname, "app/folder")path.join(__dirname, "app", "folder") 的方式来配置,以兼容 Windows 环境。