Skip to content

Commit ed9a48e

Browse files
committed
Update loader to throw same error code as Node for module not found; follow experimental-specifier-resolution behavior of searching for .mjs extensions in addition to legacy CommonJS ones
1 parent c6f86f3 commit ed9a48e

File tree

1 file changed

+16
-5
lines changed
  • commonjs-extension-resolution-loader

1 file changed

+16
-5
lines changed

commonjs-extension-resolution-loader/loader.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,22 @@ export async function resolve(specifier, context, next) {
2424
}
2525
const parentPath = fileURLToPath(parentURL);
2626

27-
const resolution = await resolveAsync(specifier, {
28-
basedir: dirname(parentPath),
29-
extensions: ['.js', '.json', '.node'],
30-
});
31-
const url = pathToFileURL(resolution).href;
27+
let url;
28+
try {
29+
const resolution = await resolveAsync(specifier, {
30+
basedir: dirname(parentPath),
31+
// For whatever reason, --experimental-specifier-resolution=node doesn't search for .mjs extensions
32+
// but it does search for index.mjs files within directories
33+
extensions: ['.js', '.json', '.node', '.mjs'],
34+
});
35+
url = pathToFileURL(resolution).href;
36+
} catch (error) {
37+
if (error.code === 'MODULE_NOT_FOUND') {
38+
// Match Node's error code
39+
error.code = 'ERR_MODULE_NOT_FOUND';
40+
}
41+
throw error;
42+
}
3243

3344
return next(url, context);
3445
}

0 commit comments

Comments
 (0)