module.d.ts (8602B)
1 /** 2 * @since v0.3.7 3 */ 4 declare module "module" { 5 import { URL } from "node:url"; 6 import { MessagePort } from "node:worker_threads"; 7 namespace Module { 8 /** 9 * The `module.syncBuiltinESMExports()` method updates all the live bindings for 10 * builtin `ES Modules` to match the properties of the `CommonJS` exports. It 11 * does not add or remove exported names from the `ES Modules`. 12 * 13 * ```js 14 * import fs from 'node:fs'; 15 * import assert from 'node:assert'; 16 * import { syncBuiltinESMExports } from 'node:module'; 17 * 18 * fs.readFile = newAPI; 19 * 20 * delete fs.readFileSync; 21 * 22 * function newAPI() { 23 * // ... 24 * } 25 * 26 * fs.newAPI = newAPI; 27 * 28 * syncBuiltinESMExports(); 29 * 30 * import('fs').then((esmFS) => { 31 * // It syncs the existing readFile property with the new value 32 * assert.strictEqual(esmFS.readFile, newAPI); 33 * // readFileSync has been deleted from the required fs 34 * assert.strictEqual('readFileSync' in fs, false); 35 * // syncBuiltinESMExports() does not remove readFileSync from esmFS 36 * assert.strictEqual('readFileSync' in esmFS, true); 37 * // syncBuiltinESMExports() does not add names 38 * assert.strictEqual(esmFS.newAPI, undefined); 39 * }); 40 * ``` 41 * @since v12.12.0 42 */ 43 function syncBuiltinESMExports(): void; 44 /** 45 * `path` is the resolved path for the file for which a corresponding source map 46 * should be fetched. 47 * @since v13.7.0, v12.17.0 48 */ 49 function findSourceMap(path: string, error?: Error): SourceMap; 50 interface SourceMapPayload { 51 file: string; 52 version: number; 53 sources: string[]; 54 sourcesContent: string[]; 55 names: string[]; 56 mappings: string; 57 sourceRoot: string; 58 } 59 interface SourceMapping { 60 generatedLine: number; 61 generatedColumn: number; 62 originalSource: string; 63 originalLine: number; 64 originalColumn: number; 65 } 66 /** 67 * @since v13.7.0, v12.17.0 68 */ 69 class SourceMap { 70 /** 71 * Getter for the payload used to construct the `SourceMap` instance. 72 */ 73 readonly payload: SourceMapPayload; 74 constructor(payload: SourceMapPayload); 75 /** 76 * Given a line number and column number in the generated source file, returns 77 * an object representing the position in the original file. The object returned 78 * consists of the following keys: 79 */ 80 findEntry(line: number, column: number): SourceMapping; 81 } 82 interface ImportAssertions extends NodeJS.Dict<string> { 83 type?: string | undefined; 84 } 85 type ModuleFormat = "builtin" | "commonjs" | "json" | "module" | "wasm"; 86 type ModuleSource = string | ArrayBuffer | NodeJS.TypedArray; 87 interface GlobalPreloadContext { 88 port: MessagePort; 89 } 90 /** 91 * Sometimes it might be necessary to run some code inside of the same global scope that the application runs in. 92 * This hook allows the return of a string that is run as a sloppy-mode script on startup. 93 * 94 * @param context Information to assist the preload code 95 * @return Code to run before application startup 96 */ 97 type GlobalPreloadHook = (context: GlobalPreloadContext) => string; 98 interface ResolveHookContext { 99 /** 100 * Export conditions of the relevant `package.json` 101 */ 102 conditions: string[]; 103 /** 104 * An object whose key-value pairs represent the assertions for the module to import 105 */ 106 importAssertions: ImportAssertions; 107 /** 108 * The module importing this one, or undefined if this is the Node.js entry point 109 */ 110 parentURL: string | undefined; 111 } 112 interface ResolveFnOutput { 113 /** 114 * A hint to the load hook (it might be ignored) 115 */ 116 format?: ModuleFormat | null | undefined; 117 /** 118 * The import assertions to use when caching the module (optional; if excluded the input will be used) 119 */ 120 importAssertions?: ImportAssertions | undefined; 121 /** 122 * A signal that this hook intends to terminate the chain of `resolve` hooks. 123 * @default false 124 */ 125 shortCircuit?: boolean | undefined; 126 /** 127 * The absolute URL to which this input resolves 128 */ 129 url: string; 130 } 131 /** 132 * The `resolve` hook chain is responsible for resolving file URL for a given module specifier and parent URL, and optionally its format (such as `'module'`) as a hint to the `load` hook. 133 * If a format is specified, the load hook is ultimately responsible for providing the final `format` value (and it is free to ignore the hint provided by `resolve`); 134 * if `resolve` provides a format, a custom `load` hook is required even if only to pass the value to the Node.js default `load` hook. 135 * 136 * @param specifier The specified URL path of the module to be resolved 137 * @param context 138 * @param nextResolve The subsequent `resolve` hook in the chain, or the Node.js default `resolve` hook after the last user-supplied resolve hook 139 */ 140 type ResolveHook = ( 141 specifier: string, 142 context: ResolveHookContext, 143 nextResolve: ( 144 specifier: string, 145 context?: ResolveHookContext, 146 ) => ResolveFnOutput | Promise<ResolveFnOutput>, 147 ) => ResolveFnOutput | Promise<ResolveFnOutput>; 148 interface LoadHookContext { 149 /** 150 * Export conditions of the relevant `package.json` 151 */ 152 conditions: string[]; 153 /** 154 * The format optionally supplied by the `resolve` hook chain 155 */ 156 format: ModuleFormat; 157 /** 158 * An object whose key-value pairs represent the assertions for the module to import 159 */ 160 importAssertions: ImportAssertions; 161 } 162 interface LoadFnOutput { 163 format: ModuleFormat; 164 /** 165 * A signal that this hook intends to terminate the chain of `resolve` hooks. 166 * @default false 167 */ 168 shortCircuit?: boolean | undefined; 169 /** 170 * The source for Node.js to evaluate 171 */ 172 source?: ModuleSource; 173 } 174 /** 175 * The `load` hook provides a way to define a custom method of determining how a URL should be interpreted, retrieved, and parsed. 176 * It is also in charge of validating the import assertion. 177 * 178 * @param url The URL/path of the module to be loaded 179 * @param context Metadata about the module 180 * @param nextLoad The subsequent `load` hook in the chain, or the Node.js default `load` hook after the last user-supplied `load` hook 181 */ 182 type LoadHook = ( 183 url: string, 184 context: LoadHookContext, 185 nextLoad: (url: string, context?: LoadHookContext) => LoadFnOutput | Promise<LoadFnOutput>, 186 ) => LoadFnOutput | Promise<LoadFnOutput>; 187 } 188 interface Module extends NodeModule {} 189 class Module { 190 static runMain(): void; 191 static wrap(code: string): string; 192 static createRequire(path: string | URL): NodeRequire; 193 static builtinModules: string[]; 194 static isBuiltin(moduleName: string): boolean; 195 static Module: typeof Module; 196 constructor(id: string, parent?: Module); 197 } 198 type ImportMetaDOMCompat = typeof globalThis extends { onmessage: any } ? { 199 resolve(specifier: string): string; 200 } 201 : { 202 resolve?(specifier: string, parent?: string | URL): Promise<string>; 203 }; 204 global { 205 interface ImportMeta extends ImportMetaDOMCompat { 206 url: string; 207 } 208 } 209 export = Module; 210 } 211 declare module "node:module" { 212 import module = require("module"); 213 export = module; 214 }