ModuleNotFoundError.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. * @param {Module | null} module module tied to dependency
  42. * @param {Error&any} err error thrown
  43. * @param {DependencyLocation} loc location of dependency
  44. */
  45. constructor(module, err, loc) {
  46. let message = `Module not found: ${err.toString()}`;
  47. // TODO remove in webpack 6
  48. const match = err.message.match(/Can't resolve '([^']+)'/);
  49. if (match) {
  50. const request = match[1];
  51. const alias =
  52. previouslyPolyfilledBuiltinModules[
  53. /** @type {keyof previouslyPolyfilledBuiltinModules} */ (request)
  54. ];
  55. if (alias) {
  56. const pathIndex = alias.indexOf("/");
  57. const dependency = pathIndex > 0 ? alias.slice(0, pathIndex) : alias;
  58. message +=
  59. "\n\n" +
  60. "BREAKING CHANGE: " +
  61. "webpack < 5 used to include polyfills for node.js core modules by default.\n" +
  62. "This is no longer the case. Verify if you need this module and configure a polyfill for it.\n\n";
  63. message +=
  64. "If you want to include a polyfill, you need to:\n" +
  65. `\t- add a fallback 'resolve.fallback: { "${request}": require.resolve("${alias}") }'\n` +
  66. `\t- install '${dependency}'\n`;
  67. message +=
  68. "If you don't want to include a polyfill, you can use an empty module like this:\n" +
  69. `\tresolve.fallback: { "${request}": false }`;
  70. }
  71. }
  72. super(message);
  73. this.name = "ModuleNotFoundError";
  74. this.details = err.details;
  75. this.module = module;
  76. this.error = err;
  77. this.loc = loc;
  78. }
  79. }
  80. module.exports = ModuleNotFoundError;