ModuleNotFoundError.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const WebpackError = require("./WebpackError");
  7. /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
  8. /** @typedef {import("./Module")} Module */
  9. const previouslyPolyfilledBuiltinModules = {
  10. assert: "assert/",
  11. buffer: "buffer/",
  12. console: "console-browserify",
  13. constants: "constants-browserify",
  14. crypto: "crypto-browserify",
  15. domain: "domain-browser",
  16. events: "events/",
  17. http: "stream-http",
  18. https: "https-browserify",
  19. os: "os-browserify/browser",
  20. path: "path-browserify",
  21. punycode: "punycode/",
  22. process: "process/browser",
  23. querystring: "querystring-es3",
  24. stream: "stream-browserify",
  25. _stream_duplex: "readable-stream/duplex",
  26. _stream_passthrough: "readable-stream/passthrough",
  27. _stream_readable: "readable-stream/readable",
  28. _stream_transform: "readable-stream/transform",
  29. _stream_writable: "readable-stream/writable",
  30. string_decoder: "string_decoder/",
  31. sys: "util/",
  32. timers: "timers-browserify",
  33. tty: "tty-browserify",
  34. url: "url/",
  35. util: "util/",
  36. vm: "vm-browserify",
  37. zlib: "browserify-zlib"
  38. };
  39. class ModuleNotFoundError extends WebpackError {
  40. /**
  41. * Creates an instance of ModuleNotFoundError.
  42. * @param {Module | null} module module tied to dependency
  43. * @param {Error & { details?: string }} err error thrown
  44. * @param {DependencyLocation} loc location of dependency
  45. */
  46. constructor(module, err, loc) {
  47. let message = `Module not found: ${err.toString()}`;
  48. // TODO remove in webpack 6
  49. const match = err.message.match(/Can't resolve '([^']+)'/);
  50. if (match) {
  51. const request = match[1];
  52. const alias =
  53. previouslyPolyfilledBuiltinModules[
  54. /** @type {keyof previouslyPolyfilledBuiltinModules} */ (request)
  55. ];
  56. if (alias) {
  57. const pathIndex = alias.indexOf("/");
  58. const dependency = pathIndex > 0 ? alias.slice(0, pathIndex) : alias;
  59. message +=
  60. "\n\n" +
  61. "BREAKING CHANGE: " +
  62. "webpack < 5 used to include polyfills for node.js core modules by default.\n" +
  63. "This is no longer the case. Verify if you need this module and configure a polyfill for it.\n\n";
  64. message +=
  65. "If you want to include a polyfill, you need to:\n" +
  66. `\t- add a fallback 'resolve.fallback: { "${request}": require.resolve("${alias}") }'\n` +
  67. `\t- install '${dependency}'\n`;
  68. message +=
  69. "If you don't want to include a polyfill, you can use an empty module like this:\n" +
  70. `\tresolve.fallback: { "${request}": false }`;
  71. }
  72. }
  73. super(message);
  74. /** @type {string} */
  75. this.name = "ModuleNotFoundError";
  76. this.details = err.details;
  77. this.module = module;
  78. this.error = err;
  79. this.loc = loc;
  80. }
  81. }
  82. module.exports = ModuleNotFoundError;