Skip to content

Instantly share code, notes, and snippets.

@hyrious
Created July 27, 2026 01:58
Show Gist options
  • Select an option

  • Save hyrious/5a784a11055272335443feecf2b31464 to your computer and use it in GitHub Desktop.

Select an option

Save hyrious/5a784a11055272335443feecf2b31464 to your computer and use it in GitHub Desktop.
Enable node run TypeScript in node_modules.
import { registerHooks, stripTypeScriptTypes } from 'node:module'
const kNodeModulesRE = /^(?:.*)[\\/]node_modules[\\/]/
registerHooks({
load(url, context, nextLoad) {
if (context.format === 'module-typescript' && kNodeModulesRE.exec(url) !== null) {
const output = nextLoad(url, context)
// Assume there's no async hook before this hook.
if (output.source != null) {
const source = stripTypeScriptTypes((/** @type {Buffer} */ output.source).toString(), { sourceUrl: url })
return { format: 'module', source, shortCircuit: output.shortCircuit }
} else {
return nextLoad(url, context)
}
} else {
return nextLoad(url, context)
}
}
})
// Usage: node --no-warnings --import <this-file> node_modules/main.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment