UNPKG

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