Created
July 27, 2026 01:58
-
-
Save hyrious/5a784a11055272335443feecf2b31464 to your computer and use it in GitHub Desktop.
Enable node run TypeScript in node_modules.
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 { 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