a0836ef39081f738380ed4b0b24be13dfc0039564fcb57eeaccbac57b8e6fb3c8f1347844d8d07218d2ffdd2712013e4946fd5ea48e764ee64519c713f96a0 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /**
  2. * @since v0.3.7
  3. */
  4. declare module "module" {
  5. import { URL } from "node:url";
  6. class Module {
  7. constructor(id: string, parent?: Module);
  8. }
  9. interface Module extends NodeJS.Module {}
  10. namespace Module {
  11. export { Module };
  12. }
  13. namespace Module {
  14. /**
  15. * A list of the names of all modules provided by Node.js. Can be used to verify
  16. * if a module is maintained by a third party or not.
  17. *
  18. * Note: the list doesn't contain prefix-only modules like `node:test`.
  19. * @since v9.3.0, v8.10.0, v6.13.0
  20. */
  21. const builtinModules: readonly string[];
  22. /**
  23. * @since v12.2.0
  24. * @param path Filename to be used to construct the require
  25. * function. Must be a file URL object, file URL string, or absolute path
  26. * string.
  27. */
  28. function createRequire(path: string | URL): NodeJS.Require;
  29. namespace constants {
  30. /**
  31. * The following constants are returned as the `status` field in the object returned by
  32. * {@link enableCompileCache} to indicate the result of the attempt to enable the
  33. * [module compile cache](https://nodejs.org/docs/latest-v24.x/api/module.html#module-compile-cache).
  34. * @since v22.8.0
  35. */
  36. namespace compileCacheStatus {
  37. /**
  38. * Node.js has enabled the compile cache successfully. The directory used to store the
  39. * compile cache will be returned in the `directory` field in the
  40. * returned object.
  41. */
  42. const ENABLED: number;
  43. /**
  44. * The compile cache has already been enabled before, either by a previous call to
  45. * {@link enableCompileCache}, or by the `NODE_COMPILE_CACHE=dir`
  46. * environment variable. The directory used to store the
  47. * compile cache will be returned in the `directory` field in the
  48. * returned object.
  49. */
  50. const ALREADY_ENABLED: number;
  51. /**
  52. * Node.js fails to enable the compile cache. This can be caused by the lack of
  53. * permission to use the specified directory, or various kinds of file system errors.
  54. * The detail of the failure will be returned in the `message` field in the
  55. * returned object.
  56. */
  57. const FAILED: number;
  58. /**
  59. * Node.js cannot enable the compile cache because the environment variable
  60. * `NODE_DISABLE_COMPILE_CACHE=1` has been set.
  61. */
  62. const DISABLED: number;
  63. }
  64. }
  65. interface EnableCompileCacheResult {
  66. /**
  67. * One of the {@link constants.compileCacheStatus}
  68. */
  69. status: number;
  70. /**
  71. * If Node.js cannot enable the compile cache, this contains
  72. * the error message. Only set if `status` is `module.constants.compileCacheStatus.FAILED`.
  73. */
  74. message?: string;
  75. /**
  76. * If the compile cache is enabled, this contains the directory
  77. * where the compile cache is stored. Only set if `status` is
  78. * `module.constants.compileCacheStatus.ENABLED` or
  79. * `module.constants.compileCacheStatus.ALREADY_ENABLED`.
  80. */
  81. directory?: string;
  82. }
  83. /**
  84. * Enable [module compile cache](https://nodejs.org/docs/latest-v24.x/api/module.html#module-compile-cache)
  85. * in the current Node.js instance.
  86. *
  87. * If `cacheDir` is not specified, Node.js will either use the directory specified by the
  88. * `NODE_COMPILE_CACHE=dir` environment variable if it's set, or use
  89. * `path.join(os.tmpdir(), 'node-compile-cache')` otherwise. For general use cases, it's
  90. * recommended to call `module.enableCompileCache()` without specifying the `cacheDir`,
  91. * so that the directory can be overridden by the `NODE_COMPILE_CACHE` environment
  92. * variable when necessary.
  93. *
  94. * Since compile cache is supposed to be a quiet optimization that is not required for the
  95. * application to be functional, this method is designed to not throw any exception when the
  96. * compile cache cannot be enabled. Instead, it will return an object containing an error
  97. * message in the `message` field to aid debugging.
  98. * If compile cache is enabled successfully, the `directory` field in the returned object
  99. * contains the path to the directory where the compile cache is stored. The `status`
  100. * field in the returned object would be one of the `module.constants.compileCacheStatus`
  101. * values to indicate the result of the attempt to enable the
  102. * [module compile cache](https://nodejs.org/docs/latest-v24.x/api/module.html#module-compile-cache).
  103. *
  104. * This method only affects the current Node.js instance. To enable it in child worker threads,
  105. * either call this method in child worker threads too, or set the
  106. * `process.env.NODE_COMPILE_CACHE` value to compile cache directory so the behavior can
  107. * be inherited into the child workers. The directory can be obtained either from the
  108. * `directory` field returned by this method, or with {@link getCompileCacheDir}.
  109. * @since v22.8.0
  110. * @param cacheDir Optional path to specify the directory where the compile cache
  111. * will be stored/retrieved.
  112. */
  113. function enableCompileCache(cacheDir?: string): EnableCompileCacheResult;
  114. /**
  115. * Flush the [module compile cache](https://nodejs.org/docs/latest-v24.x/api/module.html#module-compile-cache)
  116. * accumulated from modules already loaded
  117. * in the current Node.js instance to disk. This returns after all the flushing
  118. * file system operations come to an end, no matter they succeed or not. If there
  119. * are any errors, this will fail silently, since compile cache misses should not
  120. * interfere with the actual operation of the application.
  121. * @since v22.10.0
  122. */
  123. function flushCompileCache(): void;
  124. /**
  125. * @since v22.8.0
  126. * @return Path to the [module compile cache](https://nodejs.org/docs/latest-v24.x/api/module.html#module-compile-cache)
  127. * directory if it is enabled, or `undefined` otherwise.
  128. */
  129. function getCompileCacheDir(): string | undefined;
  130. /**
  131. * ```text
  132. * /path/to/project
  133. * ├ packages/
  134. * ├ bar/
  135. * ├ bar.js
  136. * └ package.json // name = '@foo/bar'
  137. * └ qux/
  138. * ├ node_modules/
  139. * └ some-package/
  140. * └ package.json // name = 'some-package'
  141. * ├ qux.js
  142. * └ package.json // name = '@foo/qux'
  143. * ├ main.js
  144. * └ package.json // name = '@foo'
  145. * ```
  146. * ```js
  147. * // /path/to/project/packages/bar/bar.js
  148. * import { findPackageJSON } from 'node:module';
  149. *
  150. * findPackageJSON('..', import.meta.url);
  151. * // '/path/to/project/package.json'
  152. * // Same result when passing an absolute specifier instead:
  153. * findPackageJSON(new URL('../', import.meta.url));
  154. * findPackageJSON(import.meta.resolve('../'));
  155. *
  156. * findPackageJSON('some-package', import.meta.url);
  157. * // '/path/to/project/packages/bar/node_modules/some-package/package.json'
  158. * // When passing an absolute specifier, you might get a different result if the
  159. * // resolved module is inside a subfolder that has nested `package.json`.
  160. * findPackageJSON(import.meta.resolve('some-package'));
  161. * // '/path/to/project/packages/bar/node_modules/some-package/some-subfolder/package.json'
  162. *
  163. * findPackageJSON('@foo/qux', import.meta.url);
  164. * // '/path/to/project/packages/qux/package.json'
  165. * ```
  166. * @since v22.14.0
  167. * @param specifier The specifier for the module whose `package.json` to
  168. * retrieve. When passing a _bare specifier_, the `package.json` at the root of
  169. * the package is returned. When passing a _relative specifier_ or an _absolute specifier_,
  170. * the closest parent `package.json` is returned.
  171. * @param base The absolute location (`file:` URL string or FS path) of the
  172. * containing module. For CJS, use `__filename` (not `__dirname`!); for ESM, use
  173. * `import.meta.url`. You do not need to pass it if `specifier` is an _absolute specifier_.
  174. * @returns A path if the `package.json` is found. When `startLocation`
  175. * is a package, the package's root `package.json`; when a relative or unresolved, the closest
  176. * `package.json` to the `startLocation`.
  177. */
  178. function findPackageJSON(specifier: string | URL, base?: string | URL): string | undefined;
  179. /**
  180. * @since v18.6.0, v16.17.0
  181. */
  182. function isBuiltin(moduleName: string): boolean;
  183. interface RegisterOptions<Data> {
  184. /**
  185. * If you want to resolve `specifier` relative to a
  186. * base URL, such as `import.meta.url`, you can pass that URL here. This
  187. * property is ignored if the `parentURL` is supplied as the second argument.
  188. * @default 'data:'
  189. */
  190. parentURL?: string | URL | undefined;
  191. /**
  192. * Any arbitrary, cloneable JavaScript value to pass into the
  193. * {@link initialize} hook.
  194. */
  195. data?: Data | undefined;
  196. /**
  197. * [Transferable objects](https://nodejs.org/docs/latest-v24.x/api/worker_threads.html#portpostmessagevalue-transferlist)
  198. * to be passed into the `initialize` hook.
  199. */
  200. transferList?: any[] | undefined;
  201. }
  202. /* eslint-disable @definitelytyped/no-unnecessary-generics */
  203. /**
  204. * Register a module that exports hooks that customize Node.js module
  205. * resolution and loading behavior. See
  206. * [Customization hooks](https://nodejs.org/docs/latest-v24.x/api/module.html#customization-hooks).
  207. *
  208. * This feature requires `--allow-worker` if used with the
  209. * [Permission Model](https://nodejs.org/docs/latest-v24.x/api/permissions.html#permission-model).
  210. * @since v20.6.0, v18.19.0
  211. * @param specifier Customization hooks to be registered; this should be
  212. * the same string that would be passed to `import()`, except that if it is
  213. * relative, it is resolved relative to `parentURL`.
  214. * @param parentURL f you want to resolve `specifier` relative to a base
  215. * URL, such as `import.meta.url`, you can pass that URL here.
  216. */
  217. function register<Data = any>(
  218. specifier: string | URL,
  219. parentURL?: string | URL,
  220. options?: RegisterOptions<Data>,
  221. ): void;
  222. function register<Data = any>(specifier: string | URL, options?: RegisterOptions<Data>): void;
  223. interface RegisterHooksOptions {
  224. /**
  225. * See [load hook](https://nodejs.org/docs/latest-v24.x/api/module.html#loadurl-context-nextload).
  226. * @default undefined
  227. */
  228. load?: LoadHookSync | undefined;
  229. /**
  230. * See [resolve hook](https://nodejs.org/docs/latest-v24.x/api/module.html#resolvespecifier-context-nextresolve).
  231. * @default undefined
  232. */
  233. resolve?: ResolveHookSync | undefined;
  234. }
  235. interface ModuleHooks {
  236. /**
  237. * Deregister the hook instance.
  238. */
  239. deregister(): void;
  240. }
  241. /**
  242. * Register [hooks](https://nodejs.org/docs/latest-v24.x/api/module.html#customization-hooks)
  243. * that customize Node.js module resolution and loading behavior.
  244. * @since v22.15.0
  245. * @experimental
  246. */
  247. function registerHooks(options: RegisterHooksOptions): ModuleHooks;
  248. interface StripTypeScriptTypesOptions {
  249. /**
  250. * Possible values are:
  251. * * `'strip'` Only strip type annotations without performing the transformation of TypeScript features.
  252. * * `'transform'` Strip type annotations and transform TypeScript features to JavaScript.
  253. * @default 'strip'
  254. */
  255. mode?: "strip" | "transform" | undefined;
  256. /**
  257. * Only when `mode` is `'transform'`, if `true`, a source map
  258. * will be generated for the transformed code.
  259. * @default false
  260. */
  261. sourceMap?: boolean | undefined;
  262. /**
  263. * Specifies the source url used in the source map.
  264. */
  265. sourceUrl?: string | undefined;
  266. }
  267. /**
  268. * `module.stripTypeScriptTypes()` removes type annotations from TypeScript code. It
  269. * can be used to strip type annotations from TypeScript code before running it
  270. * with `vm.runInContext()` or `vm.compileFunction()`.
  271. * By default, it will throw an error if the code contains TypeScript features
  272. * that require transformation such as `Enums`,
  273. * see [type-stripping](https://nodejs.org/docs/latest-v24.x/api/typescript.md#type-stripping) for more information.
  274. * When mode is `'transform'`, it also transforms TypeScript features to JavaScript,
  275. * see [transform TypeScript features](https://nodejs.org/docs/latest-v24.x/api/typescript.md#typescript-features) for more information.
  276. * When mode is `'strip'`, source maps are not generated, because locations are preserved.
  277. * If `sourceMap` is provided, when mode is `'strip'`, an error will be thrown.
  278. *
  279. * _WARNING_: The output of this function should not be considered stable across Node.js versions,
  280. * due to changes in the TypeScript parser.
  281. *
  282. * ```js
  283. * import { stripTypeScriptTypes } from 'node:module';
  284. * const code = 'const a: number = 1;';
  285. * const strippedCode = stripTypeScriptTypes(code);
  286. * console.log(strippedCode);
  287. * // Prints: const a = 1;
  288. * ```
  289. *
  290. * If `sourceUrl` is provided, it will be used appended as a comment at the end of the output:
  291. *
  292. * ```js
  293. * import { stripTypeScriptTypes } from 'node:module';
  294. * const code = 'const a: number = 1;';
  295. * const strippedCode = stripTypeScriptTypes(code, { mode: 'strip', sourceUrl: 'source.ts' });
  296. * console.log(strippedCode);
  297. * // Prints: const a = 1\n\n//# sourceURL=source.ts;
  298. * ```
  299. *
  300. * When `mode` is `'transform'`, the code is transformed to JavaScript:
  301. *
  302. * ```js
  303. * import { stripTypeScriptTypes } from 'node:module';
  304. * const code = `
  305. * namespace MathUtil {
  306. * export const add = (a: number, b: number) => a + b;
  307. * }`;
  308. * const strippedCode = stripTypeScriptTypes(code, { mode: 'transform', sourceMap: true });
  309. * console.log(strippedCode);
  310. * // Prints:
  311. * // var MathUtil;
  312. * // (function(MathUtil) {
  313. * // MathUtil.add = (a, b)=>a + b;
  314. * // })(MathUtil || (MathUtil = {}));
  315. * // # sourceMappingURL=data:application/json;base64, ...
  316. * ```
  317. * @since v22.13.0
  318. * @param code The code to strip type annotations from.
  319. * @returns The code with type annotations stripped.
  320. */
  321. function stripTypeScriptTypes(code: string, options?: StripTypeScriptTypesOptions): string;
  322. /* eslint-enable @definitelytyped/no-unnecessary-generics */
  323. /**
  324. * The `module.syncBuiltinESMExports()` method updates all the live bindings for
  325. * builtin `ES Modules` to match the properties of the `CommonJS` exports. It
  326. * does not add or remove exported names from the `ES Modules`.
  327. *
  328. * ```js
  329. * import fs from 'node:fs';
  330. * import assert from 'node:assert';
  331. * import { syncBuiltinESMExports } from 'node:module';
  332. *
  333. * fs.readFile = newAPI;
  334. *
  335. * delete fs.readFileSync;
  336. *
  337. * function newAPI() {
  338. * // ...
  339. * }
  340. *
  341. * fs.newAPI = newAPI;
  342. *
  343. * syncBuiltinESMExports();
  344. *
  345. * import('node:fs').then((esmFS) => {
  346. * // It syncs the existing readFile property with the new value
  347. * assert.strictEqual(esmFS.readFile, newAPI);
  348. * // readFileSync has been deleted from the required fs
  349. * assert.strictEqual('readFileSync' in fs, false);
  350. * // syncBuiltinESMExports() does not remove readFileSync from esmFS
  351. * assert.strictEqual('readFileSync' in esmFS, true);
  352. * // syncBuiltinESMExports() does not add names
  353. * assert.strictEqual(esmFS.newAPI, undefined);
  354. * });
  355. * ```
  356. * @since v12.12.0
  357. */
  358. function syncBuiltinESMExports(): void;
  359. interface ImportAttributes extends NodeJS.Dict<string> {
  360. type?: string | undefined;
  361. }
  362. type ImportPhase = "source" | "evaluation";
  363. type ModuleFormat =
  364. | "addon"
  365. | "builtin"
  366. | "commonjs"
  367. | "commonjs-typescript"
  368. | "json"
  369. | "module"
  370. | "module-typescript"
  371. | "wasm";
  372. type ModuleSource = string | ArrayBuffer | NodeJS.TypedArray;
  373. /**
  374. * The `initialize` hook provides a way to define a custom function that runs in
  375. * the hooks thread when the hooks module is initialized. Initialization happens
  376. * when the hooks module is registered via {@link register}.
  377. *
  378. * This hook can receive data from a {@link register} invocation, including
  379. * ports and other transferable objects. The return value of `initialize` can be a
  380. * `Promise`, in which case it will be awaited before the main application thread
  381. * execution resumes.
  382. */
  383. type InitializeHook<Data = any> = (data: Data) => void | Promise<void>;
  384. interface ResolveHookContext {
  385. /**
  386. * Export conditions of the relevant `package.json`
  387. */
  388. conditions: string[];
  389. /**
  390. * An object whose key-value pairs represent the assertions for the module to import
  391. */
  392. importAttributes: ImportAttributes;
  393. /**
  394. * The module importing this one, or undefined if this is the Node.js entry point
  395. */
  396. parentURL: string | undefined;
  397. }
  398. interface ResolveFnOutput {
  399. /**
  400. * A hint to the load hook (it might be ignored); can be an intermediary value.
  401. */
  402. format?: string | null | undefined;
  403. /**
  404. * The import attributes to use when caching the module (optional; if excluded the input will be used)
  405. */
  406. importAttributes?: ImportAttributes | undefined;
  407. /**
  408. * A signal that this hook intends to terminate the chain of `resolve` hooks.
  409. * @default false
  410. */
  411. shortCircuit?: boolean | undefined;
  412. /**
  413. * The absolute URL to which this input resolves
  414. */
  415. url: string;
  416. }
  417. /**
  418. * The `resolve` hook chain is responsible for telling Node.js where to find and
  419. * how to cache a given `import` statement or expression, or `require` call. It can
  420. * optionally return a format (such as `'module'`) as a hint to the `load` hook. If
  421. * a format is specified, the `load` hook is ultimately responsible for providing
  422. * the final `format` value (and it is free to ignore the hint provided by
  423. * `resolve`); if `resolve` provides a `format`, a custom `load` hook is required
  424. * even if only to pass the value to the Node.js default `load` hook.
  425. */
  426. type ResolveHook = (
  427. specifier: string,
  428. context: ResolveHookContext,
  429. nextResolve: (
  430. specifier: string,
  431. context?: Partial<ResolveHookContext>,
  432. ) => ResolveFnOutput | Promise<ResolveFnOutput>,
  433. ) => ResolveFnOutput | Promise<ResolveFnOutput>;
  434. type ResolveHookSync = (
  435. specifier: string,
  436. context: ResolveHookContext,
  437. nextResolve: (
  438. specifier: string,
  439. context?: Partial<ResolveHookContext>,
  440. ) => ResolveFnOutput,
  441. ) => ResolveFnOutput;
  442. interface LoadHookContext {
  443. /**
  444. * Export conditions of the relevant `package.json`
  445. */
  446. conditions: string[];
  447. /**
  448. * The format optionally supplied by the `resolve` hook chain (can be an intermediary value).
  449. */
  450. format: string | null | undefined;
  451. /**
  452. * An object whose key-value pairs represent the assertions for the module to import
  453. */
  454. importAttributes: ImportAttributes;
  455. }
  456. interface LoadFnOutput {
  457. format: string | null | undefined;
  458. /**
  459. * A signal that this hook intends to terminate the chain of `resolve` hooks.
  460. * @default false
  461. */
  462. shortCircuit?: boolean | undefined;
  463. /**
  464. * The source for Node.js to evaluate
  465. */
  466. source?: ModuleSource | undefined;
  467. }
  468. /**
  469. * The `load` hook provides a way to define a custom method of determining how a
  470. * URL should be interpreted, retrieved, and parsed. It is also in charge of
  471. * validating the import attributes.
  472. */
  473. type LoadHook = (
  474. url: string,
  475. context: LoadHookContext,
  476. nextLoad: (
  477. url: string,
  478. context?: Partial<LoadHookContext>,
  479. ) => LoadFnOutput | Promise<LoadFnOutput>,
  480. ) => LoadFnOutput | Promise<LoadFnOutput>;
  481. type LoadHookSync = (
  482. url: string,
  483. context: LoadHookContext,
  484. nextLoad: (
  485. url: string,
  486. context?: Partial<LoadHookContext>,
  487. ) => LoadFnOutput,
  488. ) => LoadFnOutput;
  489. interface SourceMapsSupport {
  490. /**
  491. * If the source maps support is enabled
  492. */
  493. enabled: boolean;
  494. /**
  495. * If the support is enabled for files in `node_modules`.
  496. */
  497. nodeModules: boolean;
  498. /**
  499. * If the support is enabled for generated code from `eval` or `new Function`.
  500. */
  501. generatedCode: boolean;
  502. }
  503. /**
  504. * This method returns whether the [Source Map v3](https://tc39.es/ecma426/) support for stack
  505. * traces is enabled.
  506. * @since v23.7.0, v22.14.0
  507. */
  508. function getSourceMapsSupport(): SourceMapsSupport;
  509. /**
  510. * `path` is the resolved path for the file for which a corresponding source map
  511. * should be fetched.
  512. * @since v13.7.0, v12.17.0
  513. * @return Returns `module.SourceMap` if a source map is found, `undefined` otherwise.
  514. */
  515. function findSourceMap(path: string): SourceMap | undefined;
  516. interface SetSourceMapsSupportOptions {
  517. /**
  518. * If enabling the support for files in `node_modules`.
  519. * @default false
  520. */
  521. nodeModules?: boolean | undefined;
  522. /**
  523. * If enabling the support for generated code from `eval` or `new Function`.
  524. * @default false
  525. */
  526. generatedCode?: boolean | undefined;
  527. }
  528. /**
  529. * This function enables or disables the [Source Map v3](https://tc39.es/ecma426/) support for
  530. * stack traces.
  531. *
  532. * It provides same features as launching Node.js process with commandline options
  533. * `--enable-source-maps`, with additional options to alter the support for files
  534. * in `node_modules` or generated codes.
  535. *
  536. * Only source maps in JavaScript files that are loaded after source maps has been
  537. * enabled will be parsed and loaded. Preferably, use the commandline options
  538. * `--enable-source-maps` to avoid losing track of source maps of modules loaded
  539. * before this API call.
  540. * @since v23.7.0, v22.14.0
  541. */
  542. function setSourceMapsSupport(enabled: boolean, options?: SetSourceMapsSupportOptions): void;
  543. interface SourceMapConstructorOptions {
  544. /**
  545. * @since v21.0.0, v20.5.0
  546. */
  547. lineLengths?: readonly number[] | undefined;
  548. }
  549. interface SourceMapPayload {
  550. file: string;
  551. version: number;
  552. sources: string[];
  553. sourcesContent: string[];
  554. names: string[];
  555. mappings: string;
  556. sourceRoot: string;
  557. }
  558. interface SourceMapping {
  559. generatedLine: number;
  560. generatedColumn: number;
  561. originalSource: string;
  562. originalLine: number;
  563. originalColumn: number;
  564. }
  565. interface SourceOrigin {
  566. /**
  567. * The name of the range in the source map, if one was provided
  568. */
  569. name: string | undefined;
  570. /**
  571. * The file name of the original source, as reported in the SourceMap
  572. */
  573. fileName: string;
  574. /**
  575. * The 1-indexed lineNumber of the corresponding call site in the original source
  576. */
  577. lineNumber: number;
  578. /**
  579. * The 1-indexed columnNumber of the corresponding call site in the original source
  580. */
  581. columnNumber: number;
  582. }
  583. /**
  584. * @since v13.7.0, v12.17.0
  585. */
  586. class SourceMap {
  587. constructor(payload: SourceMapPayload, options?: SourceMapConstructorOptions);
  588. /**
  589. * Getter for the payload used to construct the `SourceMap` instance.
  590. */
  591. readonly payload: SourceMapPayload;
  592. /**
  593. * Given a line offset and column offset in the generated source
  594. * file, returns an object representing the SourceMap range in the
  595. * original file if found, or an empty object if not.
  596. *
  597. * The object returned contains the following keys:
  598. *
  599. * The returned value represents the raw range as it appears in the
  600. * SourceMap, based on zero-indexed offsets, _not_ 1-indexed line and
  601. * column numbers as they appear in Error messages and CallSite
  602. * objects.
  603. *
  604. * To get the corresponding 1-indexed line and column numbers from a
  605. * lineNumber and columnNumber as they are reported by Error stacks
  606. * and CallSite objects, use `sourceMap.findOrigin(lineNumber, columnNumber)`
  607. * @param lineOffset The zero-indexed line number offset in the generated source
  608. * @param columnOffset The zero-indexed column number offset in the generated source
  609. */
  610. findEntry(lineOffset: number, columnOffset: number): SourceMapping | {};
  611. /**
  612. * Given a 1-indexed `lineNumber` and `columnNumber` from a call site in the generated source,
  613. * find the corresponding call site location in the original source.
  614. *
  615. * If the `lineNumber` and `columnNumber` provided are not found in any source map,
  616. * then an empty object is returned.
  617. * @param lineNumber The 1-indexed line number of the call site in the generated source
  618. * @param columnNumber The 1-indexed column number of the call site in the generated source
  619. */
  620. findOrigin(lineNumber: number, columnNumber: number): SourceOrigin | {};
  621. }
  622. function runMain(main?: string): void;
  623. function wrap(script: string): string;
  624. }
  625. global {
  626. interface ImportMeta {
  627. /**
  628. * The directory name of the current module.
  629. *
  630. * This is the same as the `path.dirname()` of the `import.meta.filename`.
  631. *
  632. * > **Caveat**: only present on `file:` modules.
  633. * @since v21.2.0, v20.11.0
  634. */
  635. dirname: string;
  636. /**
  637. * The full absolute path and filename of the current module, with
  638. * symlinks resolved.
  639. *
  640. * This is the same as the `url.fileURLToPath()` of the `import.meta.url`.
  641. *
  642. * > **Caveat** only local modules support this property. Modules not using the
  643. * > `file:` protocol will not provide it.
  644. * @since v21.2.0, v20.11.0
  645. */
  646. filename: string;
  647. /**
  648. * The absolute `file:` URL of the module.
  649. *
  650. * This is defined exactly the same as it is in browsers providing the URL of the
  651. * current module file.
  652. *
  653. * This enables useful patterns such as relative file loading:
  654. *
  655. * ```js
  656. * import { readFileSync } from 'node:fs';
  657. * const buffer = readFileSync(new URL('./data.proto', import.meta.url));
  658. * ```
  659. */
  660. url: string;
  661. /**
  662. * `import.meta.resolve` is a module-relative resolution function scoped to
  663. * each module, returning the URL string.
  664. *
  665. * ```js
  666. * const dependencyAsset = import.meta.resolve('component-lib/asset.css');
  667. * // file:///app/node_modules/component-lib/asset.css
  668. * import.meta.resolve('./dep.js');
  669. * // file:///app/dep.js
  670. * ```
  671. *
  672. * All features of the Node.js module resolution are supported. Dependency
  673. * resolutions are subject to the permitted exports resolutions within the package.
  674. *
  675. * **Caveats**:
  676. *
  677. * * This can result in synchronous file-system operations, which
  678. * can impact performance similarly to `require.resolve`.
  679. * * This feature is not available within custom loaders (it would
  680. * create a deadlock).
  681. * @since v13.9.0, v12.16.0
  682. * @param specifier The module specifier to resolve relative to the
  683. * current module.
  684. * @param parent An optional absolute parent module URL to resolve from.
  685. * **Default:** `import.meta.url`
  686. * @returns The absolute URL string that the specifier would resolve to.
  687. */
  688. resolve(specifier: string, parent?: string | URL): string;
  689. /**
  690. * `true` when the current module is the entry point of the current process; `false` otherwise.
  691. *
  692. * Equivalent to `require.main === module` in CommonJS.
  693. *
  694. * Analogous to Python's `__name__ == "__main__"`.
  695. *
  696. * ```js
  697. * export function foo() {
  698. * return 'Hello, world';
  699. * }
  700. *
  701. * function main() {
  702. * const message = foo();
  703. * console.log(message);
  704. * }
  705. *
  706. * if (import.meta.main) main();
  707. * // `foo` can be imported from another module without possible side-effects from `main`
  708. * ```
  709. * @since v24.2.0
  710. * @experimental
  711. */
  712. main: boolean;
  713. }
  714. namespace NodeJS {
  715. interface Module {
  716. /**
  717. * The module objects required for the first time by this one.
  718. * @since v0.1.16
  719. */
  720. children: Module[];
  721. /**
  722. * The `module.exports` object is created by the `Module` system. Sometimes this is
  723. * not acceptable; many want their module to be an instance of some class. To do
  724. * this, assign the desired export object to `module.exports`.
  725. * @since v0.1.16
  726. */
  727. exports: any;
  728. /**
  729. * The fully resolved filename of the module.
  730. * @since v0.1.16
  731. */
  732. filename: string;
  733. /**
  734. * The identifier for the module. Typically this is the fully resolved
  735. * filename.
  736. * @since v0.1.16
  737. */
  738. id: string;
  739. /**
  740. * `true` if the module is running during the Node.js preload
  741. * phase.
  742. * @since v15.4.0, v14.17.0
  743. */
  744. isPreloading: boolean;
  745. /**
  746. * Whether or not the module is done loading, or is in the process of
  747. * loading.
  748. * @since v0.1.16
  749. */
  750. loaded: boolean;
  751. /**
  752. * The module that first required this one, or `null` if the current module is the
  753. * entry point of the current process, or `undefined` if the module was loaded by
  754. * something that is not a CommonJS module (e.g. REPL or `import`).
  755. * @since v0.1.16
  756. * @deprecated Please use `require.main` and `module.children` instead.
  757. */
  758. parent: Module | null | undefined;
  759. /**
  760. * The directory name of the module. This is usually the same as the
  761. * `path.dirname()` of the `module.id`.
  762. * @since v11.14.0
  763. */
  764. path: string;
  765. /**
  766. * The search paths for the module.
  767. * @since v0.4.0
  768. */
  769. paths: string[];
  770. /**
  771. * The `module.require()` method provides a way to load a module as if
  772. * `require()` was called from the original module.
  773. * @since v0.5.1
  774. */
  775. require(id: string): any;
  776. }
  777. interface Require {
  778. /**
  779. * Used to import modules, `JSON`, and local files.
  780. * @since v0.1.13
  781. */
  782. (id: string): any;
  783. /**
  784. * Modules are cached in this object when they are required. By deleting a key
  785. * value from this object, the next `require` will reload the module.
  786. * This does not apply to
  787. * [native addons](https://nodejs.org/docs/latest-v24.x/api/addons.html),
  788. * for which reloading will result in an error.
  789. * @since v0.3.0
  790. */
  791. cache: Dict<Module>;
  792. /**
  793. * Instruct `require` on how to handle certain file extensions.
  794. * @since v0.3.0
  795. * @deprecated
  796. */
  797. extensions: RequireExtensions;
  798. /**
  799. * The `Module` object representing the entry script loaded when the Node.js
  800. * process launched, or `undefined` if the entry point of the program is not a
  801. * CommonJS module.
  802. * @since v0.1.17
  803. */
  804. main: Module | undefined;
  805. /**
  806. * @since v0.3.0
  807. */
  808. resolve: RequireResolve;
  809. }
  810. /** @deprecated */
  811. interface RequireExtensions extends Dict<(module: Module, filename: string) => any> {
  812. ".js": (module: Module, filename: string) => any;
  813. ".json": (module: Module, filename: string) => any;
  814. ".node": (module: Module, filename: string) => any;
  815. }
  816. interface RequireResolveOptions {
  817. /**
  818. * Paths to resolve module location from. If present, these
  819. * paths are used instead of the default resolution paths, with the exception
  820. * of
  821. * [GLOBAL\_FOLDERS](https://nodejs.org/docs/latest-v24.x/api/modules.html#loading-from-the-global-folders)
  822. * like `$HOME/.node_modules`, which are
  823. * always included. Each of these paths is used as a starting point for
  824. * the module resolution algorithm, meaning that the `node_modules` hierarchy
  825. * is checked from this location.
  826. * @since v8.9.0
  827. */
  828. paths?: string[] | undefined;
  829. }
  830. interface RequireResolve {
  831. /**
  832. * Use the internal `require()` machinery to look up the location of a module,
  833. * but rather than loading the module, just return the resolved filename.
  834. *
  835. * If the module can not be found, a `MODULE_NOT_FOUND` error is thrown.
  836. * @since v0.3.0
  837. * @param request The module path to resolve.
  838. */
  839. (request: string, options?: RequireResolveOptions): string;
  840. /**
  841. * Returns an array containing the paths searched during resolution of `request` or
  842. * `null` if the `request` string references a core module, for example `http` or
  843. * `fs`.
  844. * @since v8.9.0
  845. * @param request The module path whose lookup paths are being retrieved.
  846. */
  847. paths(request: string): string[] | null;
  848. }
  849. }
  850. /**
  851. * The directory name of the current module. This is the same as the
  852. * `path.dirname()` of the `__filename`.
  853. * @since v0.1.27
  854. */
  855. var __dirname: string;
  856. /**
  857. * The file name of the current module. This is the current module file's absolute
  858. * path with symlinks resolved.
  859. *
  860. * For a main program this is not necessarily the same as the file name used in the
  861. * command line.
  862. * @since v0.0.1
  863. */
  864. var __filename: string;
  865. /**
  866. * The `exports` variable is available within a module's file-level scope, and is
  867. * assigned the value of `module.exports` before the module is evaluated.
  868. * @since v0.1.16
  869. */
  870. var exports: NodeJS.Module["exports"];
  871. /**
  872. * A reference to the current module.
  873. * @since v0.1.16
  874. */
  875. var module: NodeJS.Module;
  876. /**
  877. * @since v0.1.13
  878. */
  879. var require: NodeJS.Require;
  880. // Global-scope aliases for backwards compatibility with @types/node <13.0.x
  881. // TODO: consider removing in a future major version update
  882. /** @deprecated Use `NodeJS.Module` instead. */
  883. interface NodeModule extends NodeJS.Module {}
  884. /** @deprecated Use `NodeJS.Require` instead. */
  885. interface NodeRequire extends NodeJS.Require {}
  886. /** @deprecated Use `NodeJS.RequireResolve` instead. */
  887. interface RequireResolve extends NodeJS.RequireResolve {}
  888. }
  889. export = Module;
  890. }
  891. declare module "node:module" {
  892. import module = require("module");
  893. export = module;
  894. }