Skip to content

Instantly share code, notes, and snippets.

@zkochan
Last active June 22, 2025 22:47
Show Gist options
  • Select an option

  • Save zkochan/88def229a7ecbe8f3312e647a097cf68 to your computer and use it in GitHub Desktop.

Select an option

Save zkochan/88def229a7ecbe8f3312e647a097cf68 to your computer and use it in GitHub Desktop.
esm_loader.mjs
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