Last active
June 22, 2025 22:47
-
-
Save zkochan/88def229a7ecbe8f3312e647a097cf68 to your computer and use it in GitHub Desktop.
esm_loader.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { createRequire } from 'node:module' | |
| import { delimiter } from 'node:path' | |
| import { pathToFileURL } from 'node:url' | |
| const extraNodePaths = (process.env.NODE_PATH || '') | |
| .split(delimiter) | |
| .filter(Boolean) | |
| export async function resolve(specifier, context, defaultResolve) { | |
| try { | |
| return await defaultResolve(specifier, context, defaultResolve) | |
| } catch (originalError) { | |
| if (specifier.startsWith('.') || specifier.startsWith('/') || specifier.match(/^node:/)) { | |
| // Don't handle relative, absolute, or node: specifiers | |
| throw originalError | |
| } | |
| for (const basePath of extraNodePaths) { | |
| const require = createRequire(pathToFileURL(basePath).href) | |
| try { | |
| const resolved = require.resolve(specifier) | |
| return { url: pathToFileURL(resolved).href } | |
| } catch { | |
| // Skip and try next | |
| } | |
| } | |
| // Re-throw original error if not found | |
| throw originalError | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment