ModuleNotFoundError.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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} 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 = previouslyPolyfilledBuiltinModules[request];
  52. if (alias) {
  53. const pathIndex = alias.indexOf("/");
  54. const dependency = pathIndex > 0 ? alias.slice(0, pathIndex) : alias;
  55. message +=
  56. "\n\n" +
  57. "BREAKING CHANGE: " +
  58. "webpack < 5 used to include polyfills for node.js core modules by default.\n" +
  59. "This is no longer the case. Verify if you need this module and configure a polyfill for it.\n\n";
  60. message +=
  61. "If you want to include a polyfill, you need to:\n" +
  62. `\t- add a fallback 'resolve.fallback: { "${request}": require.resolve("${alias}") }'\n` +
  63. `\t- install '${dependency}'\n`;
  64. message +=
  65. "If you don't want to include a polyfill, you can use an empty module like this:\n" +
  66. `\tresolve.fallback: { "${request}": false }`;
  67. }
  68. }
  69. super(message);
  70. this.name = "ModuleNotFoundError";
  71. this.details = err.details;
  72. this.module = module;
  73. this.error = err;
  74. this.loc = loc;
  75. }
  76. }
  77. module.exports = ModuleNotFoundError;