summaryrefslogtreecommitdiff
path: root/ext/node
diff options
context:
space:
mode:
Diffstat (limited to 'ext/node')
-rw-r--r--ext/node/polyfills/01_require.js33
1 files changed, 25 insertions, 8 deletions
diff --git a/ext/node/polyfills/01_require.js b/ext/node/polyfills/01_require.js
index 27c10bf58..efde1eb79 100644
--- a/ext/node/polyfills/01_require.js
+++ b/ext/node/polyfills/01_require.js
@@ -265,9 +265,6 @@ function setupBuiltinModules() {
}
setupBuiltinModules();
-// Map used to store CJS parsing data.
-const cjsParseCache = new SafeWeakMap();
-
function pathDirname(filepath) {
if (filepath == null) {
throw new Error("Empty filepath.");
@@ -286,11 +283,10 @@ const nativeModulePolyfill = new SafeMap();
const relativeResolveCache = ObjectCreate(null);
let requireDepth = 0;
let statCache = null;
-let isPreloading = false;
let mainModule = null;
let hasBrokenOnInspectBrk = false;
let hasInspectBrk = false;
-// Are we running with --node-modules-dir flag?
+// Are we running with --node-modules-dir flag or byonm?
let usesLocalNodeModulesDir = false;
function stat(filename) {
@@ -766,9 +762,7 @@ Module._resolveFilename = function (
if (typeof options === "object" && options !== null) {
if (ArrayIsArray(options.paths)) {
- const isRelative = op_require_is_request_relative(
- request,
- );
+ const isRelative = op_require_is_request_relative(request);
if (isRelative) {
paths = options.paths;
@@ -848,6 +842,29 @@ Module._resolveFilename = function (
const err = new Error(message);
err.code = "MODULE_NOT_FOUND";
err.requireStack = requireStack;
+
+ // fallback and attempt to resolve bare specifiers using
+ // the global cache when not using --node-modules-dir
+ if (
+ !usesLocalNodeModulesDir &&
+ ArrayIsArray(options?.paths) &&
+ request[0] !== "." &&
+ request[0] !== "#" &&
+ !request.startsWith("file:///") &&
+ !op_require_is_request_relative(request) &&
+ !op_require_path_is_absolute(request)
+ ) {
+ try {
+ return Module._resolveFilename(request, parent, isMain, {
+ ...options,
+ paths: undefined,
+ });
+ } catch {
+ // ignore
+ }
+ }
+
+ // throw the original error
throw err;
};