d3ae10dab9596b309e4df77ae5dcdb54bdf0df72d94e7ed13f113cfb6cf0481695ba8e5c2731ae36de2a9d319ff8c4ba4fb17ab2ca1cf532fbea718cca429f 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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-v22.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-v22.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-v22.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-v22.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-v22.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-v22.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-v22.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-v22.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-v22.x/api/module.html#loadurl-context-nextload).
  226. * @default undefined
  227. */
  228. load?: LoadHook | undefined;
  229. /**
  230. * See [resolve hook](https://nodejs.org/docs/latest-v22.x/api/module.html#resolvespecifier-context-nextresolve).
  231. * @default undefined
  232. */
  233. resolve?: ResolveHook | 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-v22.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-v22.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-v22.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 ModuleFormat =
  363. | "builtin"
  364. | "commonjs"
  365. | "commonjs-typescript"
  366. | "json"
  367. | "module"
  368. | "module-typescript"
  369. | "wasm";
  370. type ModuleSource = string | ArrayBuffer | NodeJS.TypedArray;
  371. /**
  372. * The `initialize` hook provides a way to define a custom function that runs in
  373. * the hooks thread when the hooks module is initialized. Initialization happens
  374. * when the hooks module is registered via {@link register}.
  375. *
  376. * This hook can receive data from a {@link register} invocation, including
  377. * ports and other transferable objects. The return value of `initialize` can be a
  378. * `Promise`, in which case it will be awaited before the main application thread
  379. * execution resumes.
  380. */
  381. type InitializeHook<Data = any> = (data: Data) => void | Promise<void>;
  382. interface ResolveHookContext {
  383. /**
  384. * Export conditions of the relevant `package.json`
  385. */
  386. conditions: string[];
  387. /**
  388. * An object whose key-value pairs represent the assertions for the module to import
  389. */
  390. importAttributes: ImportAttributes;
  391. /**
  392. * The module importing this one, or undefined if this is the Node.js entry point
  393. */
  394. parentURL: string | undefined;
  395. }
  396. interface ResolveFnOutput {
  397. /**
  398. * A hint to the load hook (it might be ignored); can be an intermediary value.
  399. */
  400. format?: string | null | undefined;
  401. /**
  402. * The import attributes to use when caching the module (optional; if excluded the input will be used)
  403. */
  404. importAttributes?: ImportAttributes | undefined;
  405. /**
  406. * A signal that this hook intends to terminate the chain of `resolve` hooks.
  407. * @default false
  408. */
  409. shortCircuit?: boolean | undefined;
  410. /**
  411. * The absolute URL to which this input resolves
  412. */
  413. url: string;
  414. }
  415. /**
  416. * The `resolve` hook chain is responsible for telling Node.js where to find and
  417. * how to cache a given `import` statement or expression, or `require` call. It can
  418. * optionally return a format (such as `'module'`) as a hint to the `load` hook. If
  419. * a format is specified, the `load` hook is ultimately responsible for providing
  420. * the final `format` value (and it is free to ignore the hint provided by
  421. * `resolve`); if `resolve` provides a `format`, a custom `load` hook is required
  422. * even if only to pass the value to the Node.js default `load` hook.
  423. */
  424. type ResolveHook = (
  425. specifier: string,
  426. context: ResolveHookContext,
  427. nextResolve: (
  428. specifier: string,
  429. context?: Partial<ResolveHookContext>,
  430. ) => ResolveFnOutput | Promise<ResolveFnOutput>,
  431. ) => ResolveFnOutput | Promise<ResolveFnOutput>;
  432. interface LoadHookContext {
  433. /**
  434. * Export conditions of the relevant `package.json`
  435. */
  436. conditions: string[];
  437. /**
  438. * The format optionally supplied by the `resolve` hook chain (can be an intermediary value).
  439. */
  440. format: string | null | undefined;
  441. /**
  442. * An object whose key-value pairs represent the assertions for the module to import
  443. */
  444. importAttributes: ImportAttributes;
  445. }
  446. interface LoadFnOutput {
  447. format: ModuleFormat;
  448. /**
  449. * A signal that this hook intends to terminate the chain of `resolve` hooks.
  450. * @default false
  451. */
  452. shortCircuit?: boolean | undefined;
  453. /**
  454. * The source for Node.js to evaluate
  455. */
  456. source?: ModuleSource | undefined;
  457. }
  458. /**
  459. * The `load` hook provides a way to define a custom method of determining how a
  460. * URL should be interpreted, retrieved, and parsed. It is also in charge of
  461. * validating the import attributes.
  462. */
  463. type LoadHook = (
  464. url: string,
  465. context: LoadHookContext,
  466. nextLoad: (
  467. url: string,
  468. context?: Partial<LoadHookContext>,
  469. ) => LoadFnOutput | Promise<LoadFnOutput>,
  470. ) => LoadFnOutput | Promise<LoadFnOutput>;
  471. /**
  472. * `path` is the resolved path for the file for which a corresponding source map
  473. * should be fetched.
  474. * @since v13.7.0, v12.17.0
  475. * @return Returns `module.SourceMap` if a source map is found, `undefined` otherwise.
  476. */
  477. function findSourceMap(path: string): SourceMap | undefined;
  478. interface SourceMapConstructorOptions {
  479. /**
  480. * @since v21.0.0, v20.5.0
  481. */
  482. lineLengths?: readonly number[] | undefined;
  483. }
  484. interface SourceMapPayload {
  485. file: string;
  486. version: number;
  487. sources: string[];
  488. sourcesContent: string[];
  489. names: string[];
  490. mappings: string;
  491. sourceRoot: string;
  492. }
  493. interface SourceMapping {
  494. generatedLine: number;
  495. generatedColumn: number;
  496. originalSource: string;
  497. originalLine: number;
  498. originalColumn: number;
  499. }
  500. interface SourceOrigin {
  501. /**
  502. * The name of the range in the source map, if one was provided
  503. */
  504. name: string | undefined;
  505. /**
  506. * The file name of the original source, as reported in the SourceMap
  507. */
  508. fileName: string;
  509. /**
  510. * The 1-indexed lineNumber of the corresponding call site in the original source
  511. */
  512. lineNumber: number;
  513. /**
  514. * The 1-indexed columnNumber of the corresponding call site in the original source
  515. */
  516. columnNumber: number;
  517. }
  518. /**
  519. * @since v13.7.0, v12.17.0
  520. */
  521. class SourceMap {
  522. constructor(payload: SourceMapPayload, options?: SourceMapConstructorOptions);
  523. /**
  524. * Getter for the payload used to construct the `SourceMap` instance.
  525. */
  526. readonly payload: SourceMapPayload;
  527. /**
  528. * Given a line offset and column offset in the generated source
  529. * file, returns an object representing the SourceMap range in the
  530. * original file if found, or an empty object if not.
  531. *
  532. * The object returned contains the following keys:
  533. *
  534. * The returned value represents the raw range as it appears in the
  535. * SourceMap, based on zero-indexed offsets, _not_ 1-indexed line and
  536. * column numbers as they appear in Error messages and CallSite
  537. * objects.
  538. *
  539. * To get the corresponding 1-indexed line and column numbers from a
  540. * lineNumber and columnNumber as they are reported by Error stacks
  541. * and CallSite objects, use `sourceMap.findOrigin(lineNumber, columnNumber)`
  542. * @param lineOffset The zero-indexed line number offset in the generated source
  543. * @param columnOffset The zero-indexed column number offset in the generated source
  544. */
  545. findEntry(lineOffset: number, columnOffset: number): SourceMapping | {};
  546. /**
  547. * Given a 1-indexed `lineNumber` and `columnNumber` from a call site in the generated source,
  548. * find the corresponding call site location in the original source.
  549. *
  550. * If the `lineNumber` and `columnNumber` provided are not found in any source map,
  551. * then an empty object is returned.
  552. * @param lineNumber The 1-indexed line number of the call site in the generated source
  553. * @param columnNumber The 1-indexed column number of the call site in the generated source
  554. */
  555. findOrigin(lineNumber: number, columnNumber: number): SourceOrigin | {};
  556. }
  557. function runMain(main?: string): void;
  558. function wrap(script: string): string;
  559. }
  560. global {
  561. interface ImportMeta {
  562. /**
  563. * The directory name of the current module. This is the same as the `path.dirname()` of the `import.meta.filename`.
  564. * **Caveat:** only present on `file:` modules.
  565. */
  566. dirname: string;
  567. /**
  568. * The full absolute path and filename of the current module, with symlinks resolved.
  569. * This is the same as the `url.fileURLToPath()` of the `import.meta.url`.
  570. * **Caveat:** only local modules support this property. Modules not using the `file:` protocol will not provide it.
  571. */
  572. filename: string;
  573. /**
  574. * The absolute `file:` URL of the module.
  575. */
  576. url: string;
  577. /**
  578. * Provides a module-relative resolution function scoped to each module, returning
  579. * the URL string.
  580. *
  581. * Second `parent` parameter is only used when the `--experimental-import-meta-resolve`
  582. * command flag enabled.
  583. *
  584. * @since v20.6.0
  585. *
  586. * @param specifier The module specifier to resolve relative to `parent`.
  587. * @param parent The absolute parent module URL to resolve from.
  588. * @returns The absolute (`file:`) URL string for the resolved module.
  589. */
  590. resolve(specifier: string, parent?: string | URL | undefined): string;
  591. }
  592. namespace NodeJS {
  593. interface Module {
  594. /**
  595. * The module objects required for the first time by this one.
  596. * @since v0.1.16
  597. */
  598. children: Module[];
  599. /**
  600. * The `module.exports` object is created by the `Module` system. Sometimes this is
  601. * not acceptable; many want their module to be an instance of some class. To do
  602. * this, assign the desired export object to `module.exports`.
  603. * @since v0.1.16
  604. */
  605. exports: any;
  606. /**
  607. * The fully resolved filename of the module.
  608. * @since v0.1.16
  609. */
  610. filename: string;
  611. /**
  612. * The identifier for the module. Typically this is the fully resolved
  613. * filename.
  614. * @since v0.1.16
  615. */
  616. id: string;
  617. /**
  618. * `true` if the module is running during the Node.js preload
  619. * phase.
  620. * @since v15.4.0, v14.17.0
  621. */
  622. isPreloading: boolean;
  623. /**
  624. * Whether or not the module is done loading, or is in the process of
  625. * loading.
  626. * @since v0.1.16
  627. */
  628. loaded: boolean;
  629. /**
  630. * The module that first required this one, or `null` if the current module is the
  631. * entry point of the current process, or `undefined` if the module was loaded by
  632. * something that is not a CommonJS module (e.g. REPL or `import`).
  633. * @since v0.1.16
  634. * @deprecated Please use `require.main` and `module.children` instead.
  635. */
  636. parent: Module | null | undefined;
  637. /**
  638. * The directory name of the module. This is usually the same as the
  639. * `path.dirname()` of the `module.id`.
  640. * @since v11.14.0
  641. */
  642. path: string;
  643. /**
  644. * The search paths for the module.
  645. * @since v0.4.0
  646. */
  647. paths: string[];
  648. /**
  649. * The `module.require()` method provides a way to load a module as if
  650. * `require()` was called from the original module.
  651. * @since v0.5.1
  652. */
  653. require(id: string): any;
  654. }
  655. interface Require {
  656. /**
  657. * Used to import modules, `JSON`, and local files.
  658. * @since v0.1.13
  659. */
  660. (id: string): any;
  661. /**
  662. * Modules are cached in this object when they are required. By deleting a key
  663. * value from this object, the next `require` will reload the module.
  664. * This does not apply to
  665. * [native addons](https://nodejs.org/docs/latest-v22.x/api/addons.html),
  666. * for which reloading will result in an error.
  667. * @since v0.3.0
  668. */
  669. cache: Dict<Module>;
  670. /**
  671. * Instruct `require` on how to handle certain file extensions.
  672. * @since v0.3.0
  673. * @deprecated
  674. */
  675. extensions: RequireExtensions;
  676. /**
  677. * The `Module` object representing the entry script loaded when the Node.js
  678. * process launched, or `undefined` if the entry point of the program is not a
  679. * CommonJS module.
  680. * @since v0.1.17
  681. */
  682. main: Module | undefined;
  683. /**
  684. * @since v0.3.0
  685. */
  686. resolve: RequireResolve;
  687. }
  688. /** @deprecated */
  689. interface RequireExtensions extends Dict<(module: Module, filename: string) => any> {
  690. ".js": (module: Module, filename: string) => any;
  691. ".json": (module: Module, filename: string) => any;
  692. ".node": (module: Module, filename: string) => any;
  693. }
  694. interface RequireResolveOptions {
  695. /**
  696. * Paths to resolve module location from. If present, these
  697. * paths are used instead of the default resolution paths, with the exception
  698. * of
  699. * [GLOBAL\_FOLDERS](https://nodejs.org/docs/latest-v22.x/api/modules.html#loading-from-the-global-folders)
  700. * like `$HOME/.node_modules`, which are
  701. * always included. Each of these paths is used as a starting point for
  702. * the module resolution algorithm, meaning that the `node_modules` hierarchy
  703. * is checked from this location.
  704. * @since v8.9.0
  705. */
  706. paths?: string[] | undefined;
  707. }
  708. interface RequireResolve {
  709. /**
  710. * Use the internal `require()` machinery to look up the location of a module,
  711. * but rather than loading the module, just return the resolved filename.
  712. *
  713. * If the module can not be found, a `MODULE_NOT_FOUND` error is thrown.
  714. * @since v0.3.0
  715. * @param request The module path to resolve.
  716. */
  717. (request: string, options?: RequireResolveOptions): string;
  718. /**
  719. * Returns an array containing the paths searched during resolution of `request` or
  720. * `null` if the `request` string references a core module, for example `http` or
  721. * `fs`.
  722. * @since v8.9.0
  723. * @param request The module path whose lookup paths are being retrieved.
  724. */
  725. paths(request: string): string[] | null;
  726. }
  727. }
  728. /**
  729. * The directory name of the current module. This is the same as the
  730. * `path.dirname()` of the `__filename`.
  731. * @since v0.1.27
  732. */
  733. var __dirname: string;
  734. /**
  735. * The file name of the current module. This is the current module file's absolute
  736. * path with symlinks resolved.
  737. *
  738. * For a main program this is not necessarily the same as the file name used in the
  739. * command line.
  740. * @since v0.0.1
  741. */
  742. var __filename: string;
  743. /**
  744. * The `exports` variable is available within a module's file-level scope, and is
  745. * assigned the value of `module.exports` before the module is evaluated.
  746. * @since v0.1.16
  747. */
  748. var exports: NodeJS.Module["exports"];
  749. /**
  750. * A reference to the current module.
  751. * @since v0.1.16
  752. */
  753. var module: NodeJS.Module;
  754. /**
  755. * @since v0.1.13
  756. */
  757. var require: NodeJS.Require;
  758. // Global-scope aliases for backwards compatibility with @types/node <13.0.x
  759. // TODO: consider removing in a future major version update
  760. /** @deprecated Use `NodeJS.Module` instead. */
  761. interface NodeModule extends NodeJS.Module {}
  762. /** @deprecated Use `NodeJS.Require` instead. */
  763. interface NodeRequire extends NodeJS.Require {}
  764. /** @deprecated Use `NodeJS.RequireResolve` instead. */
  765. interface RequireResolve extends NodeJS.RequireResolve {}
  766. }
  767. export = Module;
  768. }
  769. declare module "node:module" {
  770. import module = require("module");
  771. export = module;
  772. }