fs.d.ts (176191B)
1 /** 2 * The `fs` module enables interacting with the file system in a 3 * way modeled on standard POSIX functions. 4 * 5 * To use the promise-based APIs: 6 * 7 * ```js 8 * import * as fs from 'fs/promises'; 9 * ``` 10 * 11 * To use the callback and sync APIs: 12 * 13 * ```js 14 * import * as fs from 'fs'; 15 * ``` 16 * 17 * All file system operations have synchronous, callback, and promise-based 18 * forms, and are accessible using both CommonJS syntax and ES6 Modules (ESM). 19 * @see [source](https://github.com/nodejs/node/blob/v16.9.0/lib/fs.js) 20 */ 21 declare module "fs" { 22 import * as stream from "node:stream"; 23 import { Abortable, EventEmitter } from "node:events"; 24 import { URL } from "node:url"; 25 import * as promises from "node:fs/promises"; 26 export { promises }; 27 /** 28 * Valid types for path values in "fs". 29 */ 30 export type PathLike = string | Buffer | URL; 31 export type PathOrFileDescriptor = PathLike | number; 32 export type TimeLike = string | number | Date; 33 export type NoParamCallback = (err: NodeJS.ErrnoException | null) => void; 34 export type BufferEncodingOption = 35 | "buffer" 36 | { 37 encoding: "buffer"; 38 }; 39 export interface ObjectEncodingOptions { 40 encoding?: BufferEncoding | null | undefined; 41 } 42 export type EncodingOption = ObjectEncodingOptions | BufferEncoding | undefined | null; 43 export type OpenMode = number | string; 44 export type Mode = number | string; 45 export interface StatsBase<T> { 46 isFile(): boolean; 47 isDirectory(): boolean; 48 isBlockDevice(): boolean; 49 isCharacterDevice(): boolean; 50 isSymbolicLink(): boolean; 51 isFIFO(): boolean; 52 isSocket(): boolean; 53 dev: T; 54 ino: T; 55 mode: T; 56 nlink: T; 57 uid: T; 58 gid: T; 59 rdev: T; 60 size: T; 61 blksize: T; 62 blocks: T; 63 atimeMs: T; 64 mtimeMs: T; 65 ctimeMs: T; 66 birthtimeMs: T; 67 atime: Date; 68 mtime: Date; 69 ctime: Date; 70 birthtime: Date; 71 } 72 export interface Stats extends StatsBase<number> {} 73 /** 74 * A `fs.Stats` object provides information about a file. 75 * 76 * Objects returned from {@link stat}, {@link lstat} and {@link fstat} and 77 * their synchronous counterparts are of this type. 78 * If `bigint` in the `options` passed to those methods is true, the numeric values 79 * will be `bigint` instead of `number`, and the object will contain additional 80 * nanosecond-precision properties suffixed with `Ns`. 81 * 82 * ```console 83 * Stats { 84 * dev: 2114, 85 * ino: 48064969, 86 * mode: 33188, 87 * nlink: 1, 88 * uid: 85, 89 * gid: 100, 90 * rdev: 0, 91 * size: 527, 92 * blksize: 4096, 93 * blocks: 8, 94 * atimeMs: 1318289051000.1, 95 * mtimeMs: 1318289051000.1, 96 * ctimeMs: 1318289051000.1, 97 * birthtimeMs: 1318289051000.1, 98 * atime: Mon, 10 Oct 2011 23:24:11 GMT, 99 * mtime: Mon, 10 Oct 2011 23:24:11 GMT, 100 * ctime: Mon, 10 Oct 2011 23:24:11 GMT, 101 * birthtime: Mon, 10 Oct 2011 23:24:11 GMT } 102 * ``` 103 * 104 * `bigint` version: 105 * 106 * ```console 107 * BigIntStats { 108 * dev: 2114n, 109 * ino: 48064969n, 110 * mode: 33188n, 111 * nlink: 1n, 112 * uid: 85n, 113 * gid: 100n, 114 * rdev: 0n, 115 * size: 527n, 116 * blksize: 4096n, 117 * blocks: 8n, 118 * atimeMs: 1318289051000n, 119 * mtimeMs: 1318289051000n, 120 * ctimeMs: 1318289051000n, 121 * birthtimeMs: 1318289051000n, 122 * atimeNs: 1318289051000000000n, 123 * mtimeNs: 1318289051000000000n, 124 * ctimeNs: 1318289051000000000n, 125 * birthtimeNs: 1318289051000000000n, 126 * atime: Mon, 10 Oct 2011 23:24:11 GMT, 127 * mtime: Mon, 10 Oct 2011 23:24:11 GMT, 128 * ctime: Mon, 10 Oct 2011 23:24:11 GMT, 129 * birthtime: Mon, 10 Oct 2011 23:24:11 GMT } 130 * ``` 131 * @since v0.1.21 132 */ 133 export class Stats {} 134 /** 135 * A representation of a directory entry, which can be a file or a subdirectory 136 * within the directory, as returned by reading from an `fs.Dir`. The 137 * directory entry is a combination of the file name and file type pairs. 138 * 139 * Additionally, when {@link readdir} or {@link readdirSync} is called with 140 * the `withFileTypes` option set to `true`, the resulting array is filled with `fs.Dirent` objects, rather than strings or `Buffer` s. 141 * @since v10.10.0 142 */ 143 export class Dirent { 144 /** 145 * Returns `true` if the `fs.Dirent` object describes a regular file. 146 * @since v10.10.0 147 */ 148 isFile(): boolean; 149 /** 150 * Returns `true` if the `fs.Dirent` object describes a file system 151 * directory. 152 * @since v10.10.0 153 */ 154 isDirectory(): boolean; 155 /** 156 * Returns `true` if the `fs.Dirent` object describes a block device. 157 * @since v10.10.0 158 */ 159 isBlockDevice(): boolean; 160 /** 161 * Returns `true` if the `fs.Dirent` object describes a character device. 162 * @since v10.10.0 163 */ 164 isCharacterDevice(): boolean; 165 /** 166 * Returns `true` if the `fs.Dirent` object describes a symbolic link. 167 * @since v10.10.0 168 */ 169 isSymbolicLink(): boolean; 170 /** 171 * Returns `true` if the `fs.Dirent` object describes a first-in-first-out 172 * (FIFO) pipe. 173 * @since v10.10.0 174 */ 175 isFIFO(): boolean; 176 /** 177 * Returns `true` if the `fs.Dirent` object describes a socket. 178 * @since v10.10.0 179 */ 180 isSocket(): boolean; 181 /** 182 * The file name that this `fs.Dirent` object refers to. The type of this 183 * value is determined by the `options.encoding` passed to {@link readdir} or {@link readdirSync}. 184 * @since v10.10.0 185 */ 186 name: string; 187 } 188 /** 189 * A class representing a directory stream. 190 * 191 * Created by {@link opendir}, {@link opendirSync}, or `fsPromises.opendir()`. 192 * 193 * ```js 194 * import { opendir } from 'fs/promises'; 195 * 196 * try { 197 * const dir = await opendir('./'); 198 * for await (const dirent of dir) 199 * console.log(dirent.name); 200 * } catch (err) { 201 * console.error(err); 202 * } 203 * ``` 204 * 205 * When using the async iterator, the `fs.Dir` object will be automatically 206 * closed after the iterator exits. 207 * @since v12.12.0 208 */ 209 export class Dir implements AsyncIterable<Dirent> { 210 /** 211 * The read-only path of this directory as was provided to {@link opendir},{@link opendirSync}, or `fsPromises.opendir()`. 212 * @since v12.12.0 213 */ 214 readonly path: string; 215 /** 216 * Asynchronously iterates over the directory via `readdir(3)` until all entries have been read. 217 */ 218 [Symbol.asyncIterator](): NodeJS.AsyncIterator<Dirent>; 219 /** 220 * Asynchronously close the directory's underlying resource handle. 221 * Subsequent reads will result in errors. 222 * 223 * A promise is returned that will be resolved after the resource has been 224 * closed. 225 * @since v12.12.0 226 */ 227 close(): Promise<void>; 228 close(cb: NoParamCallback): void; 229 /** 230 * Synchronously close the directory's underlying resource handle. 231 * Subsequent reads will result in errors. 232 * @since v12.12.0 233 */ 234 closeSync(): void; 235 /** 236 * Asynchronously read the next directory entry via [`readdir(3)`](http://man7.org/linux/man-pages/man3/readdir.3.html) as an `fs.Dirent`. 237 * 238 * A promise is returned that will be resolved with an `fs.Dirent`, or `null` if there are no more directory entries to read. 239 * 240 * Directory entries returned by this function are in no particular order as 241 * provided by the operating system's underlying directory mechanisms. 242 * Entries added or removed while iterating over the directory might not be 243 * included in the iteration results. 244 * @since v12.12.0 245 * @return containing {fs.Dirent|null} 246 */ 247 read(): Promise<Dirent | null>; 248 read(cb: (err: NodeJS.ErrnoException | null, dirEnt: Dirent | null) => void): void; 249 /** 250 * Synchronously read the next directory entry as an `fs.Dirent`. See the 251 * POSIX [`readdir(3)`](http://man7.org/linux/man-pages/man3/readdir.3.html) documentation for more detail. 252 * 253 * If there are no more directory entries to read, `null` will be returned. 254 * 255 * Directory entries returned by this function are in no particular order as 256 * provided by the operating system's underlying directory mechanisms. 257 * Entries added or removed while iterating over the directory might not be 258 * included in the iteration results. 259 * @since v12.12.0 260 */ 261 readSync(): Dirent | null; 262 } 263 /** 264 * Class: fs.StatWatcher 265 * @since v14.3.0, v12.20.0 266 * Extends `EventEmitter` 267 * A successful call to {@link watchFile} method will return a new fs.StatWatcher object. 268 */ 269 export interface StatWatcher extends EventEmitter { 270 /** 271 * @since v14.3.0, v12.20.0 272 * When called, requests that the Node.js event loop not exit so long as the `fs.StatWatcher` is active. 273 * Calling `watcher.ref()` multiple times will have no effect. 274 * By default, all `fs.StatWatcher`` objects are "ref'ed", making it normally unnecessary to call `watcher.ref()` 275 * unless `watcher.unref()` had been called previously. 276 */ 277 ref(): this; 278 /** 279 * @since v14.3.0, v12.20.0 280 * When called, the active `fs.StatWatcher`` object will not require the Node.js event loop to remain active. 281 * If there is no other activity keeping the event loop running, the process may exit before the `fs.StatWatcher`` object's callback is invoked. 282 * `Calling watcher.unref()` multiple times will have no effect. 283 */ 284 unref(): this; 285 } 286 export interface FSWatcher extends EventEmitter { 287 /** 288 * Stop watching for changes on the given `fs.FSWatcher`. Once stopped, the `fs.FSWatcher` object is no longer usable. 289 * @since v0.5.8 290 */ 291 close(): void; 292 /** 293 * When called, requests that the Node.js event loop _not_ exit so long as the `fs.FSWatcher` is active. Calling `watcher.ref()` multiple times will have 294 * no effect. 295 * 296 * By default, all `fs.FSWatcher` objects are "ref'ed", making it normally 297 * unnecessary to call `watcher.ref()` unless `watcher.unref()` had been 298 * called previously. 299 * @since v14.3.0, v12.20.0 300 */ 301 ref(): this; 302 /** 303 * When called, the active `fs.FSWatcher` object will not require the Node.js 304 * event loop to remain active. If there is no other activity keeping the 305 * event loop running, the process may exit before the `fs.FSWatcher` object's 306 * callback is invoked. Calling `watcher.unref()` multiple times will have 307 * no effect. 308 * @since v14.3.0, v12.20.0 309 */ 310 unref(): this; 311 /** 312 * events.EventEmitter 313 * 1. change 314 * 2. close 315 * 3. error 316 */ 317 addListener(event: string, listener: (...args: any[]) => void): this; 318 addListener(event: "change", listener: (eventType: string, filename: string | Buffer) => void): this; 319 addListener(event: "close", listener: () => void): this; 320 addListener(event: "error", listener: (error: Error) => void): this; 321 on(event: string, listener: (...args: any[]) => void): this; 322 on(event: "change", listener: (eventType: string, filename: string | Buffer) => void): this; 323 on(event: "close", listener: () => void): this; 324 on(event: "error", listener: (error: Error) => void): this; 325 once(event: string, listener: (...args: any[]) => void): this; 326 once(event: "change", listener: (eventType: string, filename: string | Buffer) => void): this; 327 once(event: "close", listener: () => void): this; 328 once(event: "error", listener: (error: Error) => void): this; 329 prependListener(event: string, listener: (...args: any[]) => void): this; 330 prependListener(event: "change", listener: (eventType: string, filename: string | Buffer) => void): this; 331 prependListener(event: "close", listener: () => void): this; 332 prependListener(event: "error", listener: (error: Error) => void): this; 333 prependOnceListener(event: string, listener: (...args: any[]) => void): this; 334 prependOnceListener(event: "change", listener: (eventType: string, filename: string | Buffer) => void): this; 335 prependOnceListener(event: "close", listener: () => void): this; 336 prependOnceListener(event: "error", listener: (error: Error) => void): this; 337 } 338 /** 339 * Instances of `fs.ReadStream` are created and returned using the {@link createReadStream} function. 340 * @since v0.1.93 341 */ 342 export class ReadStream extends stream.Readable { 343 close(callback?: (err?: NodeJS.ErrnoException | null) => void): void; 344 /** 345 * The number of bytes that have been read so far. 346 * @since v6.4.0 347 */ 348 bytesRead: number; 349 /** 350 * The path to the file the stream is reading from as specified in the first 351 * argument to `fs.createReadStream()`. If `path` is passed as a string, then`readStream.path` will be a string. If `path` is passed as a `Buffer`, then`readStream.path` will be a 352 * `Buffer`. If `fd` is specified, then`readStream.path` will be `undefined`. 353 * @since v0.1.93 354 */ 355 path: string | Buffer; 356 /** 357 * This property is `true` if the underlying file has not been opened yet, 358 * i.e. before the `'ready'` event is emitted. 359 * @since v11.2.0, v10.16.0 360 */ 361 pending: boolean; 362 /** 363 * events.EventEmitter 364 * 1. open 365 * 2. close 366 * 3. ready 367 */ 368 addListener(event: "close", listener: () => void): this; 369 addListener(event: "data", listener: (chunk: Buffer | string) => void): this; 370 addListener(event: "end", listener: () => void): this; 371 addListener(event: "error", listener: (err: Error) => void): this; 372 addListener(event: "open", listener: (fd: number) => void): this; 373 addListener(event: "pause", listener: () => void): this; 374 addListener(event: "readable", listener: () => void): this; 375 addListener(event: "ready", listener: () => void): this; 376 addListener(event: "resume", listener: () => void): this; 377 addListener(event: string | symbol, listener: (...args: any[]) => void): this; 378 on(event: "close", listener: () => void): this; 379 on(event: "data", listener: (chunk: Buffer | string) => void): this; 380 on(event: "end", listener: () => void): this; 381 on(event: "error", listener: (err: Error) => void): this; 382 on(event: "open", listener: (fd: number) => void): this; 383 on(event: "pause", listener: () => void): this; 384 on(event: "readable", listener: () => void): this; 385 on(event: "ready", listener: () => void): this; 386 on(event: "resume", listener: () => void): this; 387 on(event: string | symbol, listener: (...args: any[]) => void): this; 388 once(event: "close", listener: () => void): this; 389 once(event: "data", listener: (chunk: Buffer | string) => void): this; 390 once(event: "end", listener: () => void): this; 391 once(event: "error", listener: (err: Error) => void): this; 392 once(event: "open", listener: (fd: number) => void): this; 393 once(event: "pause", listener: () => void): this; 394 once(event: "readable", listener: () => void): this; 395 once(event: "ready", listener: () => void): this; 396 once(event: "resume", listener: () => void): this; 397 once(event: string | symbol, listener: (...args: any[]) => void): this; 398 prependListener(event: "close", listener: () => void): this; 399 prependListener(event: "data", listener: (chunk: Buffer | string) => void): this; 400 prependListener(event: "end", listener: () => void): this; 401 prependListener(event: "error", listener: (err: Error) => void): this; 402 prependListener(event: "open", listener: (fd: number) => void): this; 403 prependListener(event: "pause", listener: () => void): this; 404 prependListener(event: "readable", listener: () => void): this; 405 prependListener(event: "ready", listener: () => void): this; 406 prependListener(event: "resume", listener: () => void): this; 407 prependListener(event: string | symbol, listener: (...args: any[]) => void): this; 408 prependOnceListener(event: "close", listener: () => void): this; 409 prependOnceListener(event: "data", listener: (chunk: Buffer | string) => void): this; 410 prependOnceListener(event: "end", listener: () => void): this; 411 prependOnceListener(event: "error", listener: (err: Error) => void): this; 412 prependOnceListener(event: "open", listener: (fd: number) => void): this; 413 prependOnceListener(event: "pause", listener: () => void): this; 414 prependOnceListener(event: "readable", listener: () => void): this; 415 prependOnceListener(event: "ready", listener: () => void): this; 416 prependOnceListener(event: "resume", listener: () => void): this; 417 prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this; 418 } 419 /** 420 * * Extends `stream.Writable` 421 * 422 * Instances of `fs.WriteStream` are created and returned using the {@link createWriteStream} function. 423 * @since v0.1.93 424 */ 425 export class WriteStream extends stream.Writable { 426 /** 427 * Closes `writeStream`. Optionally accepts a 428 * callback that will be executed once the `writeStream`is closed. 429 * @since v0.9.4 430 */ 431 close(callback?: (err?: NodeJS.ErrnoException | null) => void): void; 432 /** 433 * The number of bytes written so far. Does not include data that is still queued 434 * for writing. 435 * @since v0.4.7 436 */ 437 bytesWritten: number; 438 /** 439 * The path to the file the stream is writing to as specified in the first 440 * argument to {@link createWriteStream}. If `path` is passed as a string, then`writeStream.path` will be a string. If `path` is passed as a `Buffer`, then`writeStream.path` will be a 441 * `Buffer`. 442 * @since v0.1.93 443 */ 444 path: string | Buffer; 445 /** 446 * This property is `true` if the underlying file has not been opened yet, 447 * i.e. before the `'ready'` event is emitted. 448 * @since v11.2.0 449 */ 450 pending: boolean; 451 /** 452 * events.EventEmitter 453 * 1. open 454 * 2. close 455 * 3. ready 456 */ 457 addListener(event: "close", listener: () => void): this; 458 addListener(event: "drain", listener: () => void): this; 459 addListener(event: "error", listener: (err: Error) => void): this; 460 addListener(event: "finish", listener: () => void): this; 461 addListener(event: "open", listener: (fd: number) => void): this; 462 addListener(event: "pipe", listener: (src: stream.Readable) => void): this; 463 addListener(event: "ready", listener: () => void): this; 464 addListener(event: "unpipe", listener: (src: stream.Readable) => void): this; 465 addListener(event: string | symbol, listener: (...args: any[]) => void): this; 466 on(event: "close", listener: () => void): this; 467 on(event: "drain", listener: () => void): this; 468 on(event: "error", listener: (err: Error) => void): this; 469 on(event: "finish", listener: () => void): this; 470 on(event: "open", listener: (fd: number) => void): this; 471 on(event: "pipe", listener: (src: stream.Readable) => void): this; 472 on(event: "ready", listener: () => void): this; 473 on(event: "unpipe", listener: (src: stream.Readable) => void): this; 474 on(event: string | symbol, listener: (...args: any[]) => void): this; 475 once(event: "close", listener: () => void): this; 476 once(event: "drain", listener: () => void): this; 477 once(event: "error", listener: (err: Error) => void): this; 478 once(event: "finish", listener: () => void): this; 479 once(event: "open", listener: (fd: number) => void): this; 480 once(event: "pipe", listener: (src: stream.Readable) => void): this; 481 once(event: "ready", listener: () => void): this; 482 once(event: "unpipe", listener: (src: stream.Readable) => void): this; 483 once(event: string | symbol, listener: (...args: any[]) => void): this; 484 prependListener(event: "close", listener: () => void): this; 485 prependListener(event: "drain", listener: () => void): this; 486 prependListener(event: "error", listener: (err: Error) => void): this; 487 prependListener(event: "finish", listener: () => void): this; 488 prependListener(event: "open", listener: (fd: number) => void): this; 489 prependListener(event: "pipe", listener: (src: stream.Readable) => void): this; 490 prependListener(event: "ready", listener: () => void): this; 491 prependListener(event: "unpipe", listener: (src: stream.Readable) => void): this; 492 prependListener(event: string | symbol, listener: (...args: any[]) => void): this; 493 prependOnceListener(event: "close", listener: () => void): this; 494 prependOnceListener(event: "drain", listener: () => void): this; 495 prependOnceListener(event: "error", listener: (err: Error) => void): this; 496 prependOnceListener(event: "finish", listener: () => void): this; 497 prependOnceListener(event: "open", listener: (fd: number) => void): this; 498 prependOnceListener(event: "pipe", listener: (src: stream.Readable) => void): this; 499 prependOnceListener(event: "ready", listener: () => void): this; 500 prependOnceListener(event: "unpipe", listener: (src: stream.Readable) => void): this; 501 prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this; 502 } 503 /** 504 * Asynchronously rename file at `oldPath` to the pathname provided 505 * as `newPath`. In the case that `newPath` already exists, it will 506 * be overwritten. If there is a directory at `newPath`, an error will 507 * be raised instead. No arguments other than a possible exception are 508 * given to the completion callback. 509 * 510 * See also: [`rename(2)`](http://man7.org/linux/man-pages/man2/rename.2.html). 511 * 512 * ```js 513 * import { rename } from 'fs'; 514 * 515 * rename('oldFile.txt', 'newFile.txt', (err) => { 516 * if (err) throw err; 517 * console.log('Rename complete!'); 518 * }); 519 * ``` 520 * @since v0.0.2 521 */ 522 export function rename(oldPath: PathLike, newPath: PathLike, callback: NoParamCallback): void; 523 export namespace rename { 524 /** 525 * Asynchronous rename(2) - Change the name or location of a file or directory. 526 * @param oldPath A path to a file. If a URL is provided, it must use the `file:` protocol. 527 * URL support is _experimental_. 528 * @param newPath A path to a file. If a URL is provided, it must use the `file:` protocol. 529 * URL support is _experimental_. 530 */ 531 function __promisify__(oldPath: PathLike, newPath: PathLike): Promise<void>; 532 } 533 /** 534 * Renames the file from `oldPath` to `newPath`. Returns `undefined`. 535 * 536 * See the POSIX [`rename(2)`](http://man7.org/linux/man-pages/man2/rename.2.html) documentation for more details. 537 * @since v0.1.21 538 */ 539 export function renameSync(oldPath: PathLike, newPath: PathLike): void; 540 /** 541 * Truncates the file. No arguments other than a possible exception are 542 * given to the completion callback. A file descriptor can also be passed as the 543 * first argument. In this case, `fs.ftruncate()` is called. 544 * 545 * ```js 546 * import { truncate } from 'fs'; 547 * // Assuming that 'path/file.txt' is a regular file. 548 * truncate('path/file.txt', (err) => { 549 * if (err) throw err; 550 * console.log('path/file.txt was truncated'); 551 * }); 552 * ``` 553 * 554 * Passing a file descriptor is deprecated and may result in an error being thrown 555 * in the future. 556 * 557 * See the POSIX [`truncate(2)`](http://man7.org/linux/man-pages/man2/truncate.2.html) documentation for more details. 558 * @since v0.8.6 559 * @param [len=0] 560 */ 561 export function truncate(path: PathLike, len: number | undefined | null, callback: NoParamCallback): void; 562 /** 563 * Asynchronous truncate(2) - Truncate a file to a specified length. 564 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 565 */ 566 export function truncate(path: PathLike, callback: NoParamCallback): void; 567 export namespace truncate { 568 /** 569 * Asynchronous truncate(2) - Truncate a file to a specified length. 570 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 571 * @param len If not specified, defaults to `0`. 572 */ 573 function __promisify__(path: PathLike, len?: number | null): Promise<void>; 574 } 575 /** 576 * Truncates the file. Returns `undefined`. A file descriptor can also be 577 * passed as the first argument. In this case, `fs.ftruncateSync()` is called. 578 * 579 * Passing a file descriptor is deprecated and may result in an error being thrown 580 * in the future. 581 * @since v0.8.6 582 * @param [len=0] 583 */ 584 export function truncateSync(path: PathLike, len?: number | null): void; 585 /** 586 * Truncates the file descriptor. No arguments other than a possible exception are 587 * given to the completion callback. 588 * 589 * See the POSIX [`ftruncate(2)`](http://man7.org/linux/man-pages/man2/ftruncate.2.html) documentation for more detail. 590 * 591 * If the file referred to by the file descriptor was larger than `len` bytes, only 592 * the first `len` bytes will be retained in the file. 593 * 594 * For example, the following program retains only the first four bytes of the 595 * file: 596 * 597 * ```js 598 * import { open, close, ftruncate } from 'fs'; 599 * 600 * function closeFd(fd) { 601 * close(fd, (err) => { 602 * if (err) throw err; 603 * }); 604 * } 605 * 606 * open('temp.txt', 'r+', (err, fd) => { 607 * if (err) throw err; 608 * 609 * try { 610 * ftruncate(fd, 4, (err) => { 611 * closeFd(fd); 612 * if (err) throw err; 613 * }); 614 * } catch (err) { 615 * closeFd(fd); 616 * if (err) throw err; 617 * } 618 * }); 619 * ``` 620 * 621 * If the file previously was shorter than `len` bytes, it is extended, and the 622 * extended part is filled with null bytes (`'\0'`): 623 * 624 * If `len` is negative then `0` will be used. 625 * @since v0.8.6 626 * @param [len=0] 627 */ 628 export function ftruncate(fd: number, len: number | undefined | null, callback: NoParamCallback): void; 629 /** 630 * Asynchronous ftruncate(2) - Truncate a file to a specified length. 631 * @param fd A file descriptor. 632 */ 633 export function ftruncate(fd: number, callback: NoParamCallback): void; 634 export namespace ftruncate { 635 /** 636 * Asynchronous ftruncate(2) - Truncate a file to a specified length. 637 * @param fd A file descriptor. 638 * @param len If not specified, defaults to `0`. 639 */ 640 function __promisify__(fd: number, len?: number | null): Promise<void>; 641 } 642 /** 643 * Truncates the file descriptor. Returns `undefined`. 644 * 645 * For detailed information, see the documentation of the asynchronous version of 646 * this API: {@link ftruncate}. 647 * @since v0.8.6 648 * @param [len=0] 649 */ 650 export function ftruncateSync(fd: number, len?: number | null): void; 651 /** 652 * Asynchronously changes owner and group of a file. No arguments other than a 653 * possible exception are given to the completion callback. 654 * 655 * See the POSIX [`chown(2)`](http://man7.org/linux/man-pages/man2/chown.2.html) documentation for more detail. 656 * @since v0.1.97 657 */ 658 export function chown(path: PathLike, uid: number, gid: number, callback: NoParamCallback): void; 659 export namespace chown { 660 /** 661 * Asynchronous chown(2) - Change ownership of a file. 662 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 663 */ 664 function __promisify__(path: PathLike, uid: number, gid: number): Promise<void>; 665 } 666 /** 667 * Synchronously changes owner and group of a file. Returns `undefined`. 668 * This is the synchronous version of {@link chown}. 669 * 670 * See the POSIX [`chown(2)`](http://man7.org/linux/man-pages/man2/chown.2.html) documentation for more detail. 671 * @since v0.1.97 672 */ 673 export function chownSync(path: PathLike, uid: number, gid: number): void; 674 /** 675 * Sets the owner of the file. No arguments other than a possible exception are 676 * given to the completion callback. 677 * 678 * See the POSIX [`fchown(2)`](http://man7.org/linux/man-pages/man2/fchown.2.html) documentation for more detail. 679 * @since v0.4.7 680 */ 681 export function fchown(fd: number, uid: number, gid: number, callback: NoParamCallback): void; 682 export namespace fchown { 683 /** 684 * Asynchronous fchown(2) - Change ownership of a file. 685 * @param fd A file descriptor. 686 */ 687 function __promisify__(fd: number, uid: number, gid: number): Promise<void>; 688 } 689 /** 690 * Sets the owner of the file. Returns `undefined`. 691 * 692 * See the POSIX [`fchown(2)`](http://man7.org/linux/man-pages/man2/fchown.2.html) documentation for more detail. 693 * @since v0.4.7 694 * @param uid The file's new owner's user id. 695 * @param gid The file's new group's group id. 696 */ 697 export function fchownSync(fd: number, uid: number, gid: number): void; 698 /** 699 * Set the owner of the symbolic link. No arguments other than a possible 700 * exception are given to the completion callback. 701 * 702 * See the POSIX [`lchown(2)`](http://man7.org/linux/man-pages/man2/lchown.2.html) documentation for more detail. 703 */ 704 export function lchown(path: PathLike, uid: number, gid: number, callback: NoParamCallback): void; 705 export namespace lchown { 706 /** 707 * Asynchronous lchown(2) - Change ownership of a file. Does not dereference symbolic links. 708 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 709 */ 710 function __promisify__(path: PathLike, uid: number, gid: number): Promise<void>; 711 } 712 /** 713 * Set the owner for the path. Returns `undefined`. 714 * 715 * See the POSIX [`lchown(2)`](http://man7.org/linux/man-pages/man2/lchown.2.html) documentation for more details. 716 * @param uid The file's new owner's user id. 717 * @param gid The file's new group's group id. 718 */ 719 export function lchownSync(path: PathLike, uid: number, gid: number): void; 720 /** 721 * Changes the access and modification times of a file in the same way as {@link utimes}, with the difference that if the path refers to a symbolic 722 * link, then the link is not dereferenced: instead, the timestamps of the 723 * symbolic link itself are changed. 724 * 725 * No arguments other than a possible exception are given to the completion 726 * callback. 727 * @since v14.5.0, v12.19.0 728 */ 729 export function lutimes(path: PathLike, atime: TimeLike, mtime: TimeLike, callback: NoParamCallback): void; 730 export namespace lutimes { 731 /** 732 * Changes the access and modification times of a file in the same way as `fsPromises.utimes()`, 733 * with the difference that if the path refers to a symbolic link, then the link is not 734 * dereferenced: instead, the timestamps of the symbolic link itself are changed. 735 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 736 * @param atime The last access time. If a string is provided, it will be coerced to number. 737 * @param mtime The last modified time. If a string is provided, it will be coerced to number. 738 */ 739 function __promisify__(path: PathLike, atime: TimeLike, mtime: TimeLike): Promise<void>; 740 } 741 /** 742 * Change the file system timestamps of the symbolic link referenced by `path`. 743 * Returns `undefined`, or throws an exception when parameters are incorrect or 744 * the operation fails. This is the synchronous version of {@link lutimes}. 745 * @since v14.5.0, v12.19.0 746 */ 747 export function lutimesSync(path: PathLike, atime: TimeLike, mtime: TimeLike): void; 748 /** 749 * Asynchronously changes the permissions of a file. No arguments other than a 750 * possible exception are given to the completion callback. 751 * 752 * See the POSIX [`chmod(2)`](http://man7.org/linux/man-pages/man2/chmod.2.html) documentation for more detail. 753 * 754 * ```js 755 * import { chmod } from 'fs'; 756 * 757 * chmod('my_file.txt', 0o775, (err) => { 758 * if (err) throw err; 759 * console.log('The permissions for file "my_file.txt" have been changed!'); 760 * }); 761 * ``` 762 * @since v0.1.30 763 */ 764 export function chmod(path: PathLike, mode: Mode, callback: NoParamCallback): void; 765 export namespace chmod { 766 /** 767 * Asynchronous chmod(2) - Change permissions of a file. 768 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 769 * @param mode A file mode. If a string is passed, it is parsed as an octal integer. 770 */ 771 function __promisify__(path: PathLike, mode: Mode): Promise<void>; 772 } 773 /** 774 * For detailed information, see the documentation of the asynchronous version of 775 * this API: {@link chmod}. 776 * 777 * See the POSIX [`chmod(2)`](http://man7.org/linux/man-pages/man2/chmod.2.html) documentation for more detail. 778 * @since v0.6.7 779 */ 780 export function chmodSync(path: PathLike, mode: Mode): void; 781 /** 782 * Sets the permissions on the file. No arguments other than a possible exception 783 * are given to the completion callback. 784 * 785 * See the POSIX [`fchmod(2)`](http://man7.org/linux/man-pages/man2/fchmod.2.html) documentation for more detail. 786 * @since v0.4.7 787 */ 788 export function fchmod(fd: number, mode: Mode, callback: NoParamCallback): void; 789 export namespace fchmod { 790 /** 791 * Asynchronous fchmod(2) - Change permissions of a file. 792 * @param fd A file descriptor. 793 * @param mode A file mode. If a string is passed, it is parsed as an octal integer. 794 */ 795 function __promisify__(fd: number, mode: Mode): Promise<void>; 796 } 797 /** 798 * Sets the permissions on the file. Returns `undefined`. 799 * 800 * See the POSIX [`fchmod(2)`](http://man7.org/linux/man-pages/man2/fchmod.2.html) documentation for more detail. 801 * @since v0.4.7 802 */ 803 export function fchmodSync(fd: number, mode: Mode): void; 804 /** 805 * Changes the permissions on a symbolic link. No arguments other than a possible 806 * exception are given to the completion callback. 807 * 808 * This method is only implemented on macOS. 809 * 810 * See the POSIX [`lchmod(2)`](https://www.freebsd.org/cgi/man.cgi?query=lchmod&sektion=2) documentation for more detail. 811 * @deprecated Since v0.4.7 812 */ 813 export function lchmod(path: PathLike, mode: Mode, callback: NoParamCallback): void; 814 /** @deprecated */ 815 export namespace lchmod { 816 /** 817 * Asynchronous lchmod(2) - Change permissions of a file. Does not dereference symbolic links. 818 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 819 * @param mode A file mode. If a string is passed, it is parsed as an octal integer. 820 */ 821 function __promisify__(path: PathLike, mode: Mode): Promise<void>; 822 } 823 /** 824 * Changes the permissions on a symbolic link. Returns `undefined`. 825 * 826 * This method is only implemented on macOS. 827 * 828 * See the POSIX [`lchmod(2)`](https://www.freebsd.org/cgi/man.cgi?query=lchmod&sektion=2) documentation for more detail. 829 * @deprecated Since v0.4.7 830 */ 831 export function lchmodSync(path: PathLike, mode: Mode): void; 832 /** 833 * Asynchronous [`stat(2)`](http://man7.org/linux/man-pages/man2/stat.2.html). The callback gets two arguments `(err, stats)` where`stats` is an `fs.Stats` object. 834 * 835 * In case of an error, the `err.code` will be one of `Common System Errors`. 836 * 837 * Using `fs.stat()` to check for the existence of a file before calling`fs.open()`, `fs.readFile()` or `fs.writeFile()` is not recommended. 838 * Instead, user code should open/read/write the file directly and handle the 839 * error raised if the file is not available. 840 * 841 * To check if a file exists without manipulating it afterwards, {@link access} is recommended. 842 * 843 * For example, given the following directory structure: 844 * 845 * ```text 846 * - txtDir 847 * -- file.txt 848 * - app.js 849 * ``` 850 * 851 * The next program will check for the stats of the given paths: 852 * 853 * ```js 854 * import { stat } from 'fs'; 855 * 856 * const pathsToCheck = ['./txtDir', './txtDir/file.txt']; 857 * 858 * for (let i = 0; i < pathsToCheck.length; i++) { 859 * stat(pathsToCheck[i], (err, stats) => { 860 * console.log(stats.isDirectory()); 861 * console.log(stats); 862 * }); 863 * } 864 * ``` 865 * 866 * The resulting output will resemble: 867 * 868 * ```console 869 * true 870 * Stats { 871 * dev: 16777220, 872 * mode: 16877, 873 * nlink: 3, 874 * uid: 501, 875 * gid: 20, 876 * rdev: 0, 877 * blksize: 4096, 878 * ino: 14214262, 879 * size: 96, 880 * blocks: 0, 881 * atimeMs: 1561174653071.963, 882 * mtimeMs: 1561174614583.3518, 883 * ctimeMs: 1561174626623.5366, 884 * birthtimeMs: 1561174126937.2893, 885 * atime: 2019-06-22T03:37:33.072Z, 886 * mtime: 2019-06-22T03:36:54.583Z, 887 * ctime: 2019-06-22T03:37:06.624Z, 888 * birthtime: 2019-06-22T03:28:46.937Z 889 * } 890 * false 891 * Stats { 892 * dev: 16777220, 893 * mode: 33188, 894 * nlink: 1, 895 * uid: 501, 896 * gid: 20, 897 * rdev: 0, 898 * blksize: 4096, 899 * ino: 14214074, 900 * size: 8, 901 * blocks: 8, 902 * atimeMs: 1561174616618.8555, 903 * mtimeMs: 1561174614584, 904 * ctimeMs: 1561174614583.8145, 905 * birthtimeMs: 1561174007710.7478, 906 * atime: 2019-06-22T03:36:56.619Z, 907 * mtime: 2019-06-22T03:36:54.584Z, 908 * ctime: 2019-06-22T03:36:54.584Z, 909 * birthtime: 2019-06-22T03:26:47.711Z 910 * } 911 * ``` 912 * @since v0.0.2 913 */ 914 export function stat(path: PathLike, callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void): void; 915 export function stat( 916 path: PathLike, 917 options: 918 | (StatOptions & { 919 bigint?: false | undefined; 920 }) 921 | undefined, 922 callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void, 923 ): void; 924 export function stat( 925 path: PathLike, 926 options: StatOptions & { 927 bigint: true; 928 }, 929 callback: (err: NodeJS.ErrnoException | null, stats: BigIntStats) => void, 930 ): void; 931 export function stat( 932 path: PathLike, 933 options: StatOptions | undefined, 934 callback: (err: NodeJS.ErrnoException | null, stats: Stats | BigIntStats) => void, 935 ): void; 936 export namespace stat { 937 /** 938 * Asynchronous stat(2) - Get file status. 939 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 940 */ 941 function __promisify__( 942 path: PathLike, 943 options?: StatOptions & { 944 bigint?: false | undefined; 945 }, 946 ): Promise<Stats>; 947 function __promisify__( 948 path: PathLike, 949 options: StatOptions & { 950 bigint: true; 951 }, 952 ): Promise<BigIntStats>; 953 function __promisify__(path: PathLike, options?: StatOptions): Promise<Stats | BigIntStats>; 954 } 955 export interface StatSyncFn extends Function { 956 (path: PathLike, options?: undefined): Stats; 957 ( 958 path: PathLike, 959 options?: StatSyncOptions & { 960 bigint?: false | undefined; 961 throwIfNoEntry: false; 962 }, 963 ): Stats | undefined; 964 ( 965 path: PathLike, 966 options: StatSyncOptions & { 967 bigint: true; 968 throwIfNoEntry: false; 969 }, 970 ): BigIntStats | undefined; 971 ( 972 path: PathLike, 973 options?: StatSyncOptions & { 974 bigint?: false | undefined; 975 }, 976 ): Stats; 977 ( 978 path: PathLike, 979 options: StatSyncOptions & { 980 bigint: true; 981 }, 982 ): BigIntStats; 983 ( 984 path: PathLike, 985 options: StatSyncOptions & { 986 bigint: boolean; 987 throwIfNoEntry?: false | undefined; 988 }, 989 ): Stats | BigIntStats; 990 (path: PathLike, options?: StatSyncOptions): Stats | BigIntStats | undefined; 991 } 992 /** 993 * Synchronous stat(2) - Get file status. 994 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 995 */ 996 export const statSync: StatSyncFn; 997 /** 998 * Invokes the callback with the `fs.Stats` for the file descriptor. 999 * 1000 * See the POSIX [`fstat(2)`](http://man7.org/linux/man-pages/man2/fstat.2.html) documentation for more detail. 1001 * @since v0.1.95 1002 */ 1003 export function fstat(fd: number, callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void): void; 1004 export function fstat( 1005 fd: number, 1006 options: 1007 | (StatOptions & { 1008 bigint?: false | undefined; 1009 }) 1010 | undefined, 1011 callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void, 1012 ): void; 1013 export function fstat( 1014 fd: number, 1015 options: StatOptions & { 1016 bigint: true; 1017 }, 1018 callback: (err: NodeJS.ErrnoException | null, stats: BigIntStats) => void, 1019 ): void; 1020 export function fstat( 1021 fd: number, 1022 options: StatOptions | undefined, 1023 callback: (err: NodeJS.ErrnoException | null, stats: Stats | BigIntStats) => void, 1024 ): void; 1025 export namespace fstat { 1026 /** 1027 * Asynchronous fstat(2) - Get file status. 1028 * @param fd A file descriptor. 1029 */ 1030 function __promisify__( 1031 fd: number, 1032 options?: StatOptions & { 1033 bigint?: false | undefined; 1034 }, 1035 ): Promise<Stats>; 1036 function __promisify__( 1037 fd: number, 1038 options: StatOptions & { 1039 bigint: true; 1040 }, 1041 ): Promise<BigIntStats>; 1042 function __promisify__(fd: number, options?: StatOptions): Promise<Stats | BigIntStats>; 1043 } 1044 /** 1045 * Synchronous fstat(2) - Get file status. 1046 * @param fd A file descriptor. 1047 */ 1048 export function fstatSync( 1049 fd: number, 1050 options?: StatOptions & { 1051 bigint?: false | undefined; 1052 }, 1053 ): Stats; 1054 export function fstatSync( 1055 fd: number, 1056 options: StatOptions & { 1057 bigint: true; 1058 }, 1059 ): BigIntStats; 1060 export function fstatSync(fd: number, options?: StatOptions): Stats | BigIntStats; 1061 1062 /** 1063 * Retrieves the `fs.Stats` for the symbolic link referred to by the path. 1064 * The callback gets two arguments `(err, stats)` where `stats` is a `fs.Stats` object. `lstat()` is identical to `stat()`, except that if `path` is a symbolic 1065 * link, then the link itself is stat-ed, not the file that it refers to. 1066 * 1067 * See the POSIX [`lstat(2)`](http://man7.org/linux/man-pages/man2/lstat.2.html) documentation for more details. 1068 * @since v0.1.30 1069 */ 1070 export function lstat(path: PathLike, callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void): void; 1071 export function lstat( 1072 path: PathLike, 1073 options: 1074 | (StatOptions & { 1075 bigint?: false | undefined; 1076 }) 1077 | undefined, 1078 callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void, 1079 ): void; 1080 export function lstat( 1081 path: PathLike, 1082 options: StatOptions & { 1083 bigint: true; 1084 }, 1085 callback: (err: NodeJS.ErrnoException | null, stats: BigIntStats) => void, 1086 ): void; 1087 export function lstat( 1088 path: PathLike, 1089 options: StatOptions | undefined, 1090 callback: (err: NodeJS.ErrnoException | null, stats: Stats | BigIntStats) => void, 1091 ): void; 1092 export namespace lstat { 1093 /** 1094 * Asynchronous lstat(2) - Get file status. Does not dereference symbolic links. 1095 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1096 */ 1097 function __promisify__( 1098 path: PathLike, 1099 options?: StatOptions & { 1100 bigint?: false | undefined; 1101 }, 1102 ): Promise<Stats>; 1103 function __promisify__( 1104 path: PathLike, 1105 options: StatOptions & { 1106 bigint: true; 1107 }, 1108 ): Promise<BigIntStats>; 1109 function __promisify__(path: PathLike, options?: StatOptions): Promise<Stats | BigIntStats>; 1110 } 1111 /** 1112 * Synchronous lstat(2) - Get file status. Does not dereference symbolic links. 1113 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1114 */ 1115 export const lstatSync: StatSyncFn; 1116 /** 1117 * Creates a new link from the `existingPath` to the `newPath`. See the POSIX [`link(2)`](http://man7.org/linux/man-pages/man2/link.2.html) documentation for more detail. No arguments other than 1118 * a possible 1119 * exception are given to the completion callback. 1120 * @since v0.1.31 1121 */ 1122 export function link(existingPath: PathLike, newPath: PathLike, callback: NoParamCallback): void; 1123 export namespace link { 1124 /** 1125 * Asynchronous link(2) - Create a new link (also known as a hard link) to an existing file. 1126 * @param existingPath A path to a file. If a URL is provided, it must use the `file:` protocol. 1127 * @param newPath A path to a file. If a URL is provided, it must use the `file:` protocol. 1128 */ 1129 function __promisify__(existingPath: PathLike, newPath: PathLike): Promise<void>; 1130 } 1131 /** 1132 * Creates a new link from the `existingPath` to the `newPath`. See the POSIX [`link(2)`](http://man7.org/linux/man-pages/man2/link.2.html) documentation for more detail. Returns `undefined`. 1133 * @since v0.1.31 1134 */ 1135 export function linkSync(existingPath: PathLike, newPath: PathLike): void; 1136 /** 1137 * Creates the link called `path` pointing to `target`. No arguments other than a 1138 * possible exception are given to the completion callback. 1139 * 1140 * See the POSIX [`symlink(2)`](http://man7.org/linux/man-pages/man2/symlink.2.html) documentation for more details. 1141 * 1142 * The `type` argument is only available on Windows and ignored on other platforms. 1143 * It can be set to `'dir'`, `'file'`, or `'junction'`. If the `type` argument is 1144 * not set, Node.js will autodetect `target` type and use `'file'` or `'dir'`. If 1145 * the `target` does not exist, `'file'` will be used. Windows junction points 1146 * require the destination path to be absolute. When using `'junction'`, the`target` argument will automatically be normalized to absolute path. 1147 * 1148 * Relative targets are relative to the link’s parent directory. 1149 * 1150 * ```js 1151 * import { symlink } from 'fs'; 1152 * 1153 * symlink('./mew', './example/mewtwo', callback); 1154 * ``` 1155 * 1156 * The above example creates a symbolic link `mewtwo` in the `example` which points 1157 * to `mew` in the same directory: 1158 * 1159 * ```bash 1160 * $ tree example/ 1161 * example/ 1162 * ├── mew 1163 * └── mewtwo -> ./mew 1164 * ``` 1165 * @since v0.1.31 1166 */ 1167 export function symlink( 1168 target: PathLike, 1169 path: PathLike, 1170 type: symlink.Type | undefined | null, 1171 callback: NoParamCallback, 1172 ): void; 1173 /** 1174 * Asynchronous symlink(2) - Create a new symbolic link to an existing file. 1175 * @param target A path to an existing file. If a URL is provided, it must use the `file:` protocol. 1176 * @param path A path to the new symlink. If a URL is provided, it must use the `file:` protocol. 1177 */ 1178 export function symlink(target: PathLike, path: PathLike, callback: NoParamCallback): void; 1179 export namespace symlink { 1180 /** 1181 * Asynchronous symlink(2) - Create a new symbolic link to an existing file. 1182 * @param target A path to an existing file. If a URL is provided, it must use the `file:` protocol. 1183 * @param path A path to the new symlink. If a URL is provided, it must use the `file:` protocol. 1184 * @param type May be set to `'dir'`, `'file'`, or `'junction'` (default is `'file'`) and is only available on Windows (ignored on other platforms). 1185 * When using `'junction'`, the `target` argument will automatically be normalized to an absolute path. 1186 */ 1187 function __promisify__(target: PathLike, path: PathLike, type?: string | null): Promise<void>; 1188 type Type = "dir" | "file" | "junction"; 1189 } 1190 /** 1191 * Returns `undefined`. 1192 * 1193 * For detailed information, see the documentation of the asynchronous version of 1194 * this API: {@link symlink}. 1195 * @since v0.1.31 1196 */ 1197 export function symlinkSync(target: PathLike, path: PathLike, type?: symlink.Type | null): void; 1198 /** 1199 * Reads the contents of the symbolic link referred to by `path`. The callback gets 1200 * two arguments `(err, linkString)`. 1201 * 1202 * See the POSIX [`readlink(2)`](http://man7.org/linux/man-pages/man2/readlink.2.html) documentation for more details. 1203 * 1204 * The optional `options` argument can be a string specifying an encoding, or an 1205 * object with an `encoding` property specifying the character encoding to use for 1206 * the link path passed to the callback. If the `encoding` is set to `'buffer'`, 1207 * the link path returned will be passed as a `Buffer` object. 1208 * @since v0.1.31 1209 */ 1210 export function readlink( 1211 path: PathLike, 1212 options: EncodingOption, 1213 callback: (err: NodeJS.ErrnoException | null, linkString: string) => void, 1214 ): void; 1215 /** 1216 * Asynchronous readlink(2) - read value of a symbolic link. 1217 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1218 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1219 */ 1220 export function readlink( 1221 path: PathLike, 1222 options: BufferEncodingOption, 1223 callback: (err: NodeJS.ErrnoException | null, linkString: Buffer) => void, 1224 ): void; 1225 /** 1226 * Asynchronous readlink(2) - read value of a symbolic link. 1227 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1228 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1229 */ 1230 export function readlink( 1231 path: PathLike, 1232 options: EncodingOption, 1233 callback: (err: NodeJS.ErrnoException | null, linkString: string | Buffer) => void, 1234 ): void; 1235 /** 1236 * Asynchronous readlink(2) - read value of a symbolic link. 1237 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1238 */ 1239 export function readlink( 1240 path: PathLike, 1241 callback: (err: NodeJS.ErrnoException | null, linkString: string) => void, 1242 ): void; 1243 export namespace readlink { 1244 /** 1245 * Asynchronous readlink(2) - read value of a symbolic link. 1246 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1247 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1248 */ 1249 function __promisify__(path: PathLike, options?: EncodingOption): Promise<string>; 1250 /** 1251 * Asynchronous readlink(2) - read value of a symbolic link. 1252 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1253 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1254 */ 1255 function __promisify__(path: PathLike, options: BufferEncodingOption): Promise<Buffer>; 1256 /** 1257 * Asynchronous readlink(2) - read value of a symbolic link. 1258 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1259 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1260 */ 1261 function __promisify__(path: PathLike, options?: EncodingOption): Promise<string | Buffer>; 1262 } 1263 /** 1264 * Returns the symbolic link's string value. 1265 * 1266 * See the POSIX [`readlink(2)`](http://man7.org/linux/man-pages/man2/readlink.2.html) documentation for more details. 1267 * 1268 * The optional `options` argument can be a string specifying an encoding, or an 1269 * object with an `encoding` property specifying the character encoding to use for 1270 * the link path returned. If the `encoding` is set to `'buffer'`, 1271 * the link path returned will be passed as a `Buffer` object. 1272 * @since v0.1.31 1273 */ 1274 export function readlinkSync(path: PathLike, options?: EncodingOption): string; 1275 /** 1276 * Synchronous readlink(2) - read value of a symbolic link. 1277 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1278 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1279 */ 1280 export function readlinkSync(path: PathLike, options: BufferEncodingOption): Buffer; 1281 /** 1282 * Synchronous readlink(2) - read value of a symbolic link. 1283 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1284 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1285 */ 1286 export function readlinkSync(path: PathLike, options?: EncodingOption): string | Buffer; 1287 /** 1288 * Asynchronously computes the canonical pathname by resolving `.`, `..` and 1289 * symbolic links. 1290 * 1291 * A canonical pathname is not necessarily unique. Hard links and bind mounts can 1292 * expose a file system entity through many pathnames. 1293 * 1294 * This function behaves like [`realpath(3)`](http://man7.org/linux/man-pages/man3/realpath.3.html), with some exceptions: 1295 * 1296 * 1. No case conversion is performed on case-insensitive file systems. 1297 * 2. The maximum number of symbolic links is platform-independent and generally 1298 * (much) higher than what the native [`realpath(3)`](http://man7.org/linux/man-pages/man3/realpath.3.html) implementation supports. 1299 * 1300 * The `callback` gets two arguments `(err, resolvedPath)`. May use `process.cwd`to resolve relative paths. 1301 * 1302 * Only paths that can be converted to UTF8 strings are supported. 1303 * 1304 * The optional `options` argument can be a string specifying an encoding, or an 1305 * object with an `encoding` property specifying the character encoding to use for 1306 * the path passed to the callback. If the `encoding` is set to `'buffer'`, 1307 * the path returned will be passed as a `Buffer` object. 1308 * 1309 * If `path` resolves to a socket or a pipe, the function will return a system 1310 * dependent name for that object. 1311 * @since v0.1.31 1312 */ 1313 export function realpath( 1314 path: PathLike, 1315 options: EncodingOption, 1316 callback: (err: NodeJS.ErrnoException | null, resolvedPath: string) => void, 1317 ): void; 1318 /** 1319 * Asynchronous realpath(3) - return the canonicalized absolute pathname. 1320 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1321 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1322 */ 1323 export function realpath( 1324 path: PathLike, 1325 options: BufferEncodingOption, 1326 callback: (err: NodeJS.ErrnoException | null, resolvedPath: Buffer) => void, 1327 ): void; 1328 /** 1329 * Asynchronous realpath(3) - return the canonicalized absolute pathname. 1330 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1331 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1332 */ 1333 export function realpath( 1334 path: PathLike, 1335 options: EncodingOption, 1336 callback: (err: NodeJS.ErrnoException | null, resolvedPath: string | Buffer) => void, 1337 ): void; 1338 /** 1339 * Asynchronous realpath(3) - return the canonicalized absolute pathname. 1340 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1341 */ 1342 export function realpath( 1343 path: PathLike, 1344 callback: (err: NodeJS.ErrnoException | null, resolvedPath: string) => void, 1345 ): void; 1346 export namespace realpath { 1347 /** 1348 * Asynchronous realpath(3) - return the canonicalized absolute pathname. 1349 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1350 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1351 */ 1352 function __promisify__(path: PathLike, options?: EncodingOption): Promise<string>; 1353 /** 1354 * Asynchronous realpath(3) - return the canonicalized absolute pathname. 1355 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1356 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1357 */ 1358 function __promisify__(path: PathLike, options: BufferEncodingOption): Promise<Buffer>; 1359 /** 1360 * Asynchronous realpath(3) - return the canonicalized absolute pathname. 1361 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1362 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1363 */ 1364 function __promisify__(path: PathLike, options?: EncodingOption): Promise<string | Buffer>; 1365 /** 1366 * Asynchronous [`realpath(3)`](http://man7.org/linux/man-pages/man3/realpath.3.html). 1367 * 1368 * The `callback` gets two arguments `(err, resolvedPath)`. 1369 * 1370 * Only paths that can be converted to UTF8 strings are supported. 1371 * 1372 * The optional `options` argument can be a string specifying an encoding, or an 1373 * object with an `encoding` property specifying the character encoding to use for 1374 * the path passed to the callback. If the `encoding` is set to `'buffer'`, 1375 * the path returned will be passed as a `Buffer` object. 1376 * 1377 * On Linux, when Node.js is linked against musl libc, the procfs file system must 1378 * be mounted on `/proc` in order for this function to work. Glibc does not have 1379 * this restriction. 1380 * @since v9.2.0 1381 */ 1382 function native( 1383 path: PathLike, 1384 options: EncodingOption, 1385 callback: (err: NodeJS.ErrnoException | null, resolvedPath: string) => void, 1386 ): void; 1387 function native( 1388 path: PathLike, 1389 options: BufferEncodingOption, 1390 callback: (err: NodeJS.ErrnoException | null, resolvedPath: Buffer) => void, 1391 ): void; 1392 function native( 1393 path: PathLike, 1394 options: EncodingOption, 1395 callback: (err: NodeJS.ErrnoException | null, resolvedPath: string | Buffer) => void, 1396 ): void; 1397 function native( 1398 path: PathLike, 1399 callback: (err: NodeJS.ErrnoException | null, resolvedPath: string) => void, 1400 ): void; 1401 } 1402 /** 1403 * Returns the resolved pathname. 1404 * 1405 * For detailed information, see the documentation of the asynchronous version of 1406 * this API: {@link realpath}. 1407 * @since v0.1.31 1408 */ 1409 export function realpathSync(path: PathLike, options?: EncodingOption): string; 1410 /** 1411 * Synchronous realpath(3) - return the canonicalized absolute pathname. 1412 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1413 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1414 */ 1415 export function realpathSync(path: PathLike, options: BufferEncodingOption): Buffer; 1416 /** 1417 * Synchronous realpath(3) - return the canonicalized absolute pathname. 1418 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1419 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1420 */ 1421 export function realpathSync(path: PathLike, options?: EncodingOption): string | Buffer; 1422 export namespace realpathSync { 1423 function native(path: PathLike, options?: EncodingOption): string; 1424 function native(path: PathLike, options: BufferEncodingOption): Buffer; 1425 function native(path: PathLike, options?: EncodingOption): string | Buffer; 1426 } 1427 /** 1428 * Asynchronously removes a file or symbolic link. No arguments other than a 1429 * possible exception are given to the completion callback. 1430 * 1431 * ```js 1432 * import { unlink } from 'fs'; 1433 * // Assuming that 'path/file.txt' is a regular file. 1434 * unlink('path/file.txt', (err) => { 1435 * if (err) throw err; 1436 * console.log('path/file.txt was deleted'); 1437 * }); 1438 * ``` 1439 * 1440 * `fs.unlink()` will not work on a directory, empty or otherwise. To remove a 1441 * directory, use {@link rmdir}. 1442 * 1443 * See the POSIX [`unlink(2)`](http://man7.org/linux/man-pages/man2/unlink.2.html) documentation for more details. 1444 * @since v0.0.2 1445 */ 1446 export function unlink(path: PathLike, callback: NoParamCallback): void; 1447 export namespace unlink { 1448 /** 1449 * Asynchronous unlink(2) - delete a name and possibly the file it refers to. 1450 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1451 */ 1452 function __promisify__(path: PathLike): Promise<void>; 1453 } 1454 /** 1455 * Synchronous [`unlink(2)`](http://man7.org/linux/man-pages/man2/unlink.2.html). Returns `undefined`. 1456 * @since v0.1.21 1457 */ 1458 export function unlinkSync(path: PathLike): void; 1459 export interface RmDirOptions { 1460 /** 1461 * If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or 1462 * `EPERM` error is encountered, Node.js will retry the operation with a linear 1463 * backoff wait of `retryDelay` ms longer on each try. This option represents the 1464 * number of retries. This option is ignored if the `recursive` option is not 1465 * `true`. 1466 * @default 0 1467 */ 1468 maxRetries?: number | undefined; 1469 /** 1470 * @deprecated since v14.14.0 In future versions of Node.js and will trigger a warning 1471 * `fs.rmdir(path, { recursive: true })` will throw if `path` does not exist or is a file. 1472 * Use `fs.rm(path, { recursive: true, force: true })` instead. 1473 * 1474 * If `true`, perform a recursive directory removal. In 1475 * recursive mode soperations are retried on failure. 1476 * @default false 1477 */ 1478 recursive?: boolean | undefined; 1479 /** 1480 * The amount of time in milliseconds to wait between retries. 1481 * This option is ignored if the `recursive` option is not `true`. 1482 * @default 100 1483 */ 1484 retryDelay?: number | undefined; 1485 } 1486 /** 1487 * Asynchronous [`rmdir(2)`](http://man7.org/linux/man-pages/man2/rmdir.2.html). No arguments other than a possible exception are given 1488 * to the completion callback. 1489 * 1490 * Using `fs.rmdir()` on a file (not a directory) results in an `ENOENT` error on 1491 * Windows and an `ENOTDIR` error on POSIX. 1492 * 1493 * To get a behavior similar to the `rm -rf` Unix command, use {@link rm} with options `{ recursive: true, force: true }`. 1494 * @since v0.0.2 1495 */ 1496 export function rmdir(path: PathLike, callback: NoParamCallback): void; 1497 export function rmdir(path: PathLike, options: RmDirOptions, callback: NoParamCallback): void; 1498 export namespace rmdir { 1499 /** 1500 * Asynchronous rmdir(2) - delete a directory. 1501 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1502 */ 1503 function __promisify__(path: PathLike, options?: RmDirOptions): Promise<void>; 1504 } 1505 /** 1506 * Synchronous [`rmdir(2)`](http://man7.org/linux/man-pages/man2/rmdir.2.html). Returns `undefined`. 1507 * 1508 * Using `fs.rmdirSync()` on a file (not a directory) results in an `ENOENT` error 1509 * on Windows and an `ENOTDIR` error on POSIX. 1510 * 1511 * To get a behavior similar to the `rm -rf` Unix command, use {@link rmSync} with options `{ recursive: true, force: true }`. 1512 * @since v0.1.21 1513 */ 1514 export function rmdirSync(path: PathLike, options?: RmDirOptions): void; 1515 export interface RmOptions { 1516 /** 1517 * When `true`, exceptions will be ignored if `path` does not exist. 1518 * @default false 1519 */ 1520 force?: boolean | undefined; 1521 /** 1522 * If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or 1523 * `EPERM` error is encountered, Node.js will retry the operation with a linear 1524 * backoff wait of `retryDelay` ms longer on each try. This option represents the 1525 * number of retries. This option is ignored if the `recursive` option is not 1526 * `true`. 1527 * @default 0 1528 */ 1529 maxRetries?: number | undefined; 1530 /** 1531 * If `true`, perform a recursive directory removal. In 1532 * recursive mode, operations are retried on failure. 1533 * @default false 1534 */ 1535 recursive?: boolean | undefined; 1536 /** 1537 * The amount of time in milliseconds to wait between retries. 1538 * This option is ignored if the `recursive` option is not `true`. 1539 * @default 100 1540 */ 1541 retryDelay?: number | undefined; 1542 } 1543 /** 1544 * Asynchronously removes files and directories (modeled on the standard POSIX `rm`utility). No arguments other than a possible exception are given to the 1545 * completion callback. 1546 * @since v14.14.0 1547 */ 1548 export function rm(path: PathLike, callback: NoParamCallback): void; 1549 export function rm(path: PathLike, options: RmOptions, callback: NoParamCallback): void; 1550 export namespace rm { 1551 /** 1552 * Asynchronously removes files and directories (modeled on the standard POSIX `rm` utility). 1553 */ 1554 function __promisify__(path: PathLike, options?: RmOptions): Promise<void>; 1555 } 1556 /** 1557 * Synchronously removes files and directories (modeled on the standard POSIX `rm`utility). Returns `undefined`. 1558 * @since v14.14.0 1559 */ 1560 export function rmSync(path: PathLike, options?: RmOptions): void; 1561 export interface MakeDirectoryOptions { 1562 /** 1563 * Indicates whether parent folders should be created. 1564 * If a folder was created, the path to the first created folder will be returned. 1565 * @default false 1566 */ 1567 recursive?: boolean | undefined; 1568 /** 1569 * A file mode. If a string is passed, it is parsed as an octal integer. If not specified 1570 * @default 0o777 1571 */ 1572 mode?: Mode | undefined; 1573 } 1574 /** 1575 * Asynchronously creates a directory. 1576 * 1577 * The callback is given a possible exception and, if `recursive` is `true`, the 1578 * first directory path created, `(err[, path])`.`path` can still be `undefined` when `recursive` is `true`, if no directory was 1579 * created. 1580 * 1581 * The optional `options` argument can be an integer specifying `mode` (permission 1582 * and sticky bits), or an object with a `mode` property and a `recursive`property indicating whether parent directories should be created. Calling`fs.mkdir()` when `path` is a directory that 1583 * exists results in an error only 1584 * when `recursive` is false. 1585 * 1586 * ```js 1587 * import { mkdir } from 'fs'; 1588 * 1589 * // Creates /tmp/a/apple, regardless of whether `/tmp` and /tmp/a exist. 1590 * mkdir('/tmp/a/apple', { recursive: true }, (err) => { 1591 * if (err) throw err; 1592 * }); 1593 * ``` 1594 * 1595 * On Windows, using `fs.mkdir()` on the root directory even with recursion will 1596 * result in an error: 1597 * 1598 * ```js 1599 * import { mkdir } from 'fs'; 1600 * 1601 * mkdir('/', { recursive: true }, (err) => { 1602 * // => [Error: EPERM: operation not permitted, mkdir 'C:\'] 1603 * }); 1604 * ``` 1605 * 1606 * See the POSIX [`mkdir(2)`](http://man7.org/linux/man-pages/man2/mkdir.2.html) documentation for more details. 1607 * @since v0.1.8 1608 */ 1609 export function mkdir( 1610 path: PathLike, 1611 options: MakeDirectoryOptions & { 1612 recursive: true; 1613 }, 1614 callback: (err: NodeJS.ErrnoException | null, path?: string) => void, 1615 ): void; 1616 /** 1617 * Asynchronous mkdir(2) - create a directory. 1618 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1619 * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders 1620 * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`. 1621 */ 1622 export function mkdir( 1623 path: PathLike, 1624 options: 1625 | Mode 1626 | (MakeDirectoryOptions & { 1627 recursive?: false | undefined; 1628 }) 1629 | null 1630 | undefined, 1631 callback: NoParamCallback, 1632 ): void; 1633 /** 1634 * Asynchronous mkdir(2) - create a directory. 1635 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1636 * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders 1637 * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`. 1638 */ 1639 export function mkdir( 1640 path: PathLike, 1641 options: Mode | MakeDirectoryOptions | null | undefined, 1642 callback: (err: NodeJS.ErrnoException | null, path?: string) => void, 1643 ): void; 1644 /** 1645 * Asynchronous mkdir(2) - create a directory with a mode of `0o777`. 1646 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1647 */ 1648 export function mkdir(path: PathLike, callback: NoParamCallback): void; 1649 export namespace mkdir { 1650 /** 1651 * Asynchronous mkdir(2) - create a directory. 1652 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1653 * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders 1654 * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`. 1655 */ 1656 function __promisify__( 1657 path: PathLike, 1658 options: MakeDirectoryOptions & { 1659 recursive: true; 1660 }, 1661 ): Promise<string | undefined>; 1662 /** 1663 * Asynchronous mkdir(2) - create a directory. 1664 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1665 * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders 1666 * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`. 1667 */ 1668 function __promisify__( 1669 path: PathLike, 1670 options?: 1671 | Mode 1672 | (MakeDirectoryOptions & { 1673 recursive?: false | undefined; 1674 }) 1675 | null, 1676 ): Promise<void>; 1677 /** 1678 * Asynchronous mkdir(2) - create a directory. 1679 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1680 * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders 1681 * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`. 1682 */ 1683 function __promisify__( 1684 path: PathLike, 1685 options?: Mode | MakeDirectoryOptions | null, 1686 ): Promise<string | undefined>; 1687 } 1688 /** 1689 * Synchronously creates a directory. Returns `undefined`, or if `recursive` is`true`, the first directory path created. 1690 * This is the synchronous version of {@link mkdir}. 1691 * 1692 * See the POSIX [`mkdir(2)`](http://man7.org/linux/man-pages/man2/mkdir.2.html) documentation for more details. 1693 * @since v0.1.21 1694 */ 1695 export function mkdirSync( 1696 path: PathLike, 1697 options: MakeDirectoryOptions & { 1698 recursive: true; 1699 }, 1700 ): string | undefined; 1701 /** 1702 * Synchronous mkdir(2) - create a directory. 1703 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1704 * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders 1705 * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`. 1706 */ 1707 export function mkdirSync( 1708 path: PathLike, 1709 options?: 1710 | Mode 1711 | (MakeDirectoryOptions & { 1712 recursive?: false | undefined; 1713 }) 1714 | null, 1715 ): void; 1716 /** 1717 * Synchronous mkdir(2) - create a directory. 1718 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1719 * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders 1720 * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`. 1721 */ 1722 export function mkdirSync(path: PathLike, options?: Mode | MakeDirectoryOptions | null): string | undefined; 1723 /** 1724 * Creates a unique temporary directory. 1725 * 1726 * Generates six random characters to be appended behind a required`prefix` to create a unique temporary directory. Due to platform 1727 * inconsistencies, avoid trailing `X` characters in `prefix`. Some platforms, 1728 * notably the BSDs, can return more than six random characters, and replace 1729 * trailing `X` characters in `prefix` with random characters. 1730 * 1731 * The created directory path is passed as a string to the callback's second 1732 * parameter. 1733 * 1734 * The optional `options` argument can be a string specifying an encoding, or an 1735 * object with an `encoding` property specifying the character encoding to use. 1736 * 1737 * ```js 1738 * import { mkdtemp } from 'fs'; 1739 * 1740 * mkdtemp(path.join(os.tmpdir(), 'foo-'), (err, directory) => { 1741 * if (err) throw err; 1742 * console.log(directory); 1743 * // Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2 1744 * }); 1745 * ``` 1746 * 1747 * The `fs.mkdtemp()` method will append the six randomly selected characters 1748 * directly to the `prefix` string. For instance, given a directory `/tmp`, if the 1749 * intention is to create a temporary directory _within_`/tmp`, the `prefix`must end with a trailing platform-specific path separator 1750 * (`import { sep } from 'node:path'`). 1751 * 1752 * ```js 1753 * import { tmpdir } from 'os'; 1754 * import { mkdtemp } from 'fs'; 1755 * 1756 * // The parent directory for the new temporary directory 1757 * const tmpDir = tmpdir(); 1758 * 1759 * // This method is *INCORRECT*: 1760 * mkdtemp(tmpDir, (err, directory) => { 1761 * if (err) throw err; 1762 * console.log(directory); 1763 * // Will print something similar to `/tmpabc123`. 1764 * // A new temporary directory is created at the file system root 1765 * // rather than *within* the /tmp directory. 1766 * }); 1767 * 1768 * // This method is *CORRECT*: 1769 * import { sep } from 'path'; 1770 * mkdtemp(`${tmpDir}${sep}`, (err, directory) => { 1771 * if (err) throw err; 1772 * console.log(directory); 1773 * // Will print something similar to `/tmp/abc123`. 1774 * // A new temporary directory is created within 1775 * // the /tmp directory. 1776 * }); 1777 * ``` 1778 * @since v5.10.0 1779 */ 1780 export function mkdtemp( 1781 prefix: string, 1782 options: EncodingOption, 1783 callback: (err: NodeJS.ErrnoException | null, folder: string) => void, 1784 ): void; 1785 /** 1786 * Asynchronously creates a unique temporary directory. 1787 * Generates six random characters to be appended behind a required prefix to create a unique temporary directory. 1788 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1789 */ 1790 export function mkdtemp( 1791 prefix: string, 1792 options: 1793 | "buffer" 1794 | { 1795 encoding: "buffer"; 1796 }, 1797 callback: (err: NodeJS.ErrnoException | null, folder: Buffer) => void, 1798 ): void; 1799 /** 1800 * Asynchronously creates a unique temporary directory. 1801 * Generates six random characters to be appended behind a required prefix to create a unique temporary directory. 1802 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1803 */ 1804 export function mkdtemp( 1805 prefix: string, 1806 options: EncodingOption, 1807 callback: (err: NodeJS.ErrnoException | null, folder: string | Buffer) => void, 1808 ): void; 1809 /** 1810 * Asynchronously creates a unique temporary directory. 1811 * Generates six random characters to be appended behind a required prefix to create a unique temporary directory. 1812 */ 1813 export function mkdtemp( 1814 prefix: string, 1815 callback: (err: NodeJS.ErrnoException | null, folder: string) => void, 1816 ): void; 1817 export namespace mkdtemp { 1818 /** 1819 * Asynchronously creates a unique temporary directory. 1820 * Generates six random characters to be appended behind a required prefix to create a unique temporary directory. 1821 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1822 */ 1823 function __promisify__(prefix: string, options?: EncodingOption): Promise<string>; 1824 /** 1825 * Asynchronously creates a unique temporary directory. 1826 * Generates six random characters to be appended behind a required prefix to create a unique temporary directory. 1827 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1828 */ 1829 function __promisify__(prefix: string, options: BufferEncodingOption): Promise<Buffer>; 1830 /** 1831 * Asynchronously creates a unique temporary directory. 1832 * Generates six random characters to be appended behind a required prefix to create a unique temporary directory. 1833 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1834 */ 1835 function __promisify__(prefix: string, options?: EncodingOption): Promise<string | Buffer>; 1836 } 1837 /** 1838 * Returns the created directory path. 1839 * 1840 * For detailed information, see the documentation of the asynchronous version of 1841 * this API: {@link mkdtemp}. 1842 * 1843 * The optional `options` argument can be a string specifying an encoding, or an 1844 * object with an `encoding` property specifying the character encoding to use. 1845 * @since v5.10.0 1846 */ 1847 export function mkdtempSync(prefix: string, options?: EncodingOption): string; 1848 /** 1849 * Synchronously creates a unique temporary directory. 1850 * Generates six random characters to be appended behind a required prefix to create a unique temporary directory. 1851 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1852 */ 1853 export function mkdtempSync(prefix: string, options: BufferEncodingOption): Buffer; 1854 /** 1855 * Synchronously creates a unique temporary directory. 1856 * Generates six random characters to be appended behind a required prefix to create a unique temporary directory. 1857 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1858 */ 1859 export function mkdtempSync(prefix: string, options?: EncodingOption): string | Buffer; 1860 /** 1861 * Reads the contents of a directory. The callback gets two arguments `(err, files)`where `files` is an array of the names of the files in the directory excluding`'.'` and `'..'`. 1862 * 1863 * See the POSIX [`readdir(3)`](http://man7.org/linux/man-pages/man3/readdir.3.html) documentation for more details. 1864 * 1865 * The optional `options` argument can be a string specifying an encoding, or an 1866 * object with an `encoding` property specifying the character encoding to use for 1867 * the filenames passed to the callback. If the `encoding` is set to `'buffer'`, 1868 * the filenames returned will be passed as `Buffer` objects. 1869 * 1870 * If `options.withFileTypes` is set to `true`, the `files` array will contain `fs.Dirent` objects. 1871 * @since v0.1.8 1872 */ 1873 export function readdir( 1874 path: PathLike, 1875 options: 1876 | { 1877 encoding: BufferEncoding | null; 1878 withFileTypes?: false | undefined; 1879 } 1880 | BufferEncoding 1881 | undefined 1882 | null, 1883 callback: (err: NodeJS.ErrnoException | null, files: string[]) => void, 1884 ): void; 1885 /** 1886 * Asynchronous readdir(3) - read a directory. 1887 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1888 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1889 */ 1890 export function readdir( 1891 path: PathLike, 1892 options: 1893 | { 1894 encoding: "buffer"; 1895 withFileTypes?: false | undefined; 1896 } 1897 | "buffer", 1898 callback: (err: NodeJS.ErrnoException | null, files: Buffer[]) => void, 1899 ): void; 1900 /** 1901 * Asynchronous readdir(3) - read a directory. 1902 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1903 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1904 */ 1905 export function readdir( 1906 path: PathLike, 1907 options: 1908 | (ObjectEncodingOptions & { 1909 withFileTypes?: false | undefined; 1910 }) 1911 | BufferEncoding 1912 | undefined 1913 | null, 1914 callback: (err: NodeJS.ErrnoException | null, files: string[] | Buffer[]) => void, 1915 ): void; 1916 /** 1917 * Asynchronous readdir(3) - read a directory. 1918 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1919 */ 1920 export function readdir( 1921 path: PathLike, 1922 callback: (err: NodeJS.ErrnoException | null, files: string[]) => void, 1923 ): void; 1924 /** 1925 * Asynchronous readdir(3) - read a directory. 1926 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1927 * @param options If called with `withFileTypes: true` the result data will be an array of Dirent. 1928 */ 1929 export function readdir( 1930 path: PathLike, 1931 options: ObjectEncodingOptions & { 1932 withFileTypes: true; 1933 }, 1934 callback: (err: NodeJS.ErrnoException | null, files: Dirent[]) => void, 1935 ): void; 1936 export namespace readdir { 1937 /** 1938 * Asynchronous readdir(3) - read a directory. 1939 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1940 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1941 */ 1942 function __promisify__( 1943 path: PathLike, 1944 options?: 1945 | { 1946 encoding: BufferEncoding | null; 1947 withFileTypes?: false | undefined; 1948 } 1949 | BufferEncoding 1950 | null, 1951 ): Promise<string[]>; 1952 /** 1953 * Asynchronous readdir(3) - read a directory. 1954 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1955 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1956 */ 1957 function __promisify__( 1958 path: PathLike, 1959 options: 1960 | "buffer" 1961 | { 1962 encoding: "buffer"; 1963 withFileTypes?: false | undefined; 1964 }, 1965 ): Promise<Buffer[]>; 1966 /** 1967 * Asynchronous readdir(3) - read a directory. 1968 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1969 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 1970 */ 1971 function __promisify__( 1972 path: PathLike, 1973 options?: 1974 | (ObjectEncodingOptions & { 1975 withFileTypes?: false | undefined; 1976 }) 1977 | BufferEncoding 1978 | null, 1979 ): Promise<string[] | Buffer[]>; 1980 /** 1981 * Asynchronous readdir(3) - read a directory. 1982 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 1983 * @param options If called with `withFileTypes: true` the result data will be an array of Dirent 1984 */ 1985 function __promisify__( 1986 path: PathLike, 1987 options: ObjectEncodingOptions & { 1988 withFileTypes: true; 1989 }, 1990 ): Promise<Dirent[]>; 1991 } 1992 /** 1993 * Reads the contents of the directory. 1994 * 1995 * See the POSIX [`readdir(3)`](http://man7.org/linux/man-pages/man3/readdir.3.html) documentation for more details. 1996 * 1997 * The optional `options` argument can be a string specifying an encoding, or an 1998 * object with an `encoding` property specifying the character encoding to use for 1999 * the filenames returned. If the `encoding` is set to `'buffer'`, 2000 * the filenames returned will be passed as `Buffer` objects. 2001 * 2002 * If `options.withFileTypes` is set to `true`, the result will contain `fs.Dirent` objects. 2003 * @since v0.1.21 2004 */ 2005 export function readdirSync( 2006 path: PathLike, 2007 options?: 2008 | { 2009 encoding: BufferEncoding | null; 2010 withFileTypes?: false | undefined; 2011 } 2012 | BufferEncoding 2013 | null, 2014 ): string[]; 2015 /** 2016 * Synchronous readdir(3) - read a directory. 2017 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 2018 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 2019 */ 2020 export function readdirSync( 2021 path: PathLike, 2022 options: 2023 | { 2024 encoding: "buffer"; 2025 withFileTypes?: false | undefined; 2026 } 2027 | "buffer", 2028 ): Buffer[]; 2029 /** 2030 * Synchronous readdir(3) - read a directory. 2031 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 2032 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. 2033 */ 2034 export function readdirSync( 2035 path: PathLike, 2036 options?: 2037 | (ObjectEncodingOptions & { 2038 withFileTypes?: false | undefined; 2039 }) 2040 | BufferEncoding 2041 | null, 2042 ): string[] | Buffer[]; 2043 /** 2044 * Synchronous readdir(3) - read a directory. 2045 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 2046 * @param options If called with `withFileTypes: true` the result data will be an array of Dirent. 2047 */ 2048 export function readdirSync( 2049 path: PathLike, 2050 options: ObjectEncodingOptions & { 2051 withFileTypes: true; 2052 }, 2053 ): Dirent[]; 2054 /** 2055 * Closes the file descriptor. No arguments other than a possible exception are 2056 * given to the completion callback. 2057 * 2058 * Calling `fs.close()` on any file descriptor (`fd`) that is currently in use 2059 * through any other `fs` operation may lead to undefined behavior. 2060 * 2061 * See the POSIX [`close(2)`](http://man7.org/linux/man-pages/man2/close.2.html) documentation for more detail. 2062 * @since v0.0.2 2063 */ 2064 export function close(fd: number, callback?: NoParamCallback): void; 2065 export namespace close { 2066 /** 2067 * Asynchronous close(2) - close a file descriptor. 2068 * @param fd A file descriptor. 2069 */ 2070 function __promisify__(fd: number): Promise<void>; 2071 } 2072 /** 2073 * Closes the file descriptor. Returns `undefined`. 2074 * 2075 * Calling `fs.closeSync()` on any file descriptor (`fd`) that is currently in use 2076 * through any other `fs` operation may lead to undefined behavior. 2077 * 2078 * See the POSIX [`close(2)`](http://man7.org/linux/man-pages/man2/close.2.html) documentation for more detail. 2079 * @since v0.1.21 2080 */ 2081 export function closeSync(fd: number): void; 2082 /** 2083 * Asynchronous file open. See the POSIX [`open(2)`](http://man7.org/linux/man-pages/man2/open.2.html) documentation for more details. 2084 * 2085 * `mode` sets the file mode (permission and sticky bits), but only if the file was 2086 * created. On Windows, only the write permission can be manipulated; see {@link chmod}. 2087 * 2088 * The callback gets two arguments `(err, fd)`. 2089 * 2090 * Some characters (`< > : " / \ | ? *`) are reserved under Windows as documented 2091 * by [Naming Files, Paths, and Namespaces](https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file). Under NTFS, if the filename contains 2092 * a colon, Node.js will open a file system stream, as described by [this MSDN page](https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams). 2093 * 2094 * Functions based on `fs.open()` exhibit this behavior as well:`fs.writeFile()`, `fs.readFile()`, etc. 2095 * @since v0.0.2 2096 * @param [flags='r'] See `support of file system `flags``. 2097 * @param [mode=0o666] 2098 */ 2099 export function open( 2100 path: PathLike, 2101 flags: OpenMode | undefined, 2102 mode: Mode | undefined | null, 2103 callback: (err: NodeJS.ErrnoException | null, fd: number) => void, 2104 ): void; 2105 /** 2106 * Asynchronous open(2) - open and possibly create a file. If the file is created, its mode will be `0o666`. 2107 * @param [flags='r'] See `support of file system `flags``. 2108 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 2109 */ 2110 export function open( 2111 path: PathLike, 2112 flags: OpenMode | undefined, 2113 callback: (err: NodeJS.ErrnoException | null, fd: number) => void, 2114 ): void; 2115 /** 2116 * Asynchronous open(2) - open and possibly create a file. If the file is created, its mode will be `0o666`. 2117 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 2118 */ 2119 export function open(path: PathLike, callback: (err: NodeJS.ErrnoException | null, fd: number) => void): void; 2120 export namespace open { 2121 /** 2122 * Asynchronous open(2) - open and possibly create a file. 2123 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 2124 * @param mode A file mode. If a string is passed, it is parsed as an octal integer. If not supplied, defaults to `0o666`. 2125 */ 2126 function __promisify__(path: PathLike, flags: OpenMode, mode?: Mode | null): Promise<number>; 2127 } 2128 /** 2129 * Returns an integer representing the file descriptor. 2130 * 2131 * For detailed information, see the documentation of the asynchronous version of 2132 * this API: {@link open}. 2133 * @since v0.1.21 2134 * @param [flags='r'] 2135 * @param [mode=0o666] 2136 */ 2137 export function openSync(path: PathLike, flags: OpenMode, mode?: Mode | null): number; 2138 /** 2139 * Change the file system timestamps of the object referenced by `path`. 2140 * 2141 * The `atime` and `mtime` arguments follow these rules: 2142 * 2143 * * Values can be either numbers representing Unix epoch time in seconds,`Date`s, or a numeric string like `'123456789.0'`. 2144 * * If the value can not be converted to a number, or is `NaN`, `Infinity` or`-Infinity`, an `Error` will be thrown. 2145 * @since v0.4.2 2146 */ 2147 export function utimes(path: PathLike, atime: TimeLike, mtime: TimeLike, callback: NoParamCallback): void; 2148 export namespace utimes { 2149 /** 2150 * Asynchronously change file timestamps of the file referenced by the supplied path. 2151 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 2152 * @param atime The last access time. If a string is provided, it will be coerced to number. 2153 * @param mtime The last modified time. If a string is provided, it will be coerced to number. 2154 */ 2155 function __promisify__(path: PathLike, atime: TimeLike, mtime: TimeLike): Promise<void>; 2156 } 2157 /** 2158 * Returns `undefined`. 2159 * 2160 * For detailed information, see the documentation of the asynchronous version of 2161 * this API: {@link utimes}. 2162 * @since v0.4.2 2163 */ 2164 export function utimesSync(path: PathLike, atime: TimeLike, mtime: TimeLike): void; 2165 /** 2166 * Change the file system timestamps of the object referenced by the supplied file 2167 * descriptor. See {@link utimes}. 2168 * @since v0.4.2 2169 */ 2170 export function futimes(fd: number, atime: TimeLike, mtime: TimeLike, callback: NoParamCallback): void; 2171 export namespace futimes { 2172 /** 2173 * Asynchronously change file timestamps of the file referenced by the supplied file descriptor. 2174 * @param fd A file descriptor. 2175 * @param atime The last access time. If a string is provided, it will be coerced to number. 2176 * @param mtime The last modified time. If a string is provided, it will be coerced to number. 2177 */ 2178 function __promisify__(fd: number, atime: TimeLike, mtime: TimeLike): Promise<void>; 2179 } 2180 /** 2181 * Synchronous version of {@link futimes}. Returns `undefined`. 2182 * @since v0.4.2 2183 */ 2184 export function futimesSync(fd: number, atime: TimeLike, mtime: TimeLike): void; 2185 /** 2186 * Request that all data for the open file descriptor is flushed to the storage 2187 * device. The specific implementation is operating system and device specific. 2188 * Refer to the POSIX [`fsync(2)`](http://man7.org/linux/man-pages/man2/fsync.2.html) documentation for more detail. No arguments other 2189 * than a possible exception are given to the completion callback. 2190 * @since v0.1.96 2191 */ 2192 export function fsync(fd: number, callback: NoParamCallback): void; 2193 export namespace fsync { 2194 /** 2195 * Asynchronous fsync(2) - synchronize a file's in-core state with the underlying storage device. 2196 * @param fd A file descriptor. 2197 */ 2198 function __promisify__(fd: number): Promise<void>; 2199 } 2200 /** 2201 * Request that all data for the open file descriptor is flushed to the storage 2202 * device. The specific implementation is operating system and device specific. 2203 * Refer to the POSIX [`fsync(2)`](http://man7.org/linux/man-pages/man2/fsync.2.html) documentation for more detail. Returns `undefined`. 2204 * @since v0.1.96 2205 */ 2206 export function fsyncSync(fd: number): void; 2207 /** 2208 * Write `buffer` to the file specified by `fd`. If `buffer` is a normal object, it 2209 * must have an own `toString` function property. 2210 * 2211 * `offset` determines the part of the buffer to be written, and `length` is 2212 * an integer specifying the number of bytes to write. 2213 * 2214 * `position` refers to the offset from the beginning of the file where this data 2215 * should be written. If `typeof position !== 'number'`, the data will be written 2216 * at the current position. See [`pwrite(2)`](http://man7.org/linux/man-pages/man2/pwrite.2.html). 2217 * 2218 * The callback will be given three arguments `(err, bytesWritten, buffer)` where`bytesWritten` specifies how many _bytes_ were written from `buffer`. 2219 * 2220 * If this method is invoked as its `util.promisify()` ed version, it returns 2221 * a promise for an `Object` with `bytesWritten` and `buffer` properties. 2222 * 2223 * It is unsafe to use `fs.write()` multiple times on the same file without waiting 2224 * for the callback. For this scenario, {@link createWriteStream} is 2225 * recommended. 2226 * 2227 * On Linux, positional writes don't work when the file is opened in append mode. 2228 * The kernel ignores the position argument and always appends the data to 2229 * the end of the file. 2230 * @since v0.0.2 2231 */ 2232 export function write<TBuffer extends NodeJS.ArrayBufferView>( 2233 fd: number, 2234 buffer: TBuffer, 2235 offset: number | undefined | null, 2236 length: number | undefined | null, 2237 position: number | undefined | null, 2238 callback: (err: NodeJS.ErrnoException | null, written: number, buffer: TBuffer) => void, 2239 ): void; 2240 /** 2241 * Asynchronously writes `buffer` to the file referenced by the supplied file descriptor. 2242 * @param fd A file descriptor. 2243 * @param offset The part of the buffer to be written. If not supplied, defaults to `0`. 2244 * @param length The number of bytes to write. If not supplied, defaults to `buffer.length - offset`. 2245 */ 2246 export function write<TBuffer extends NodeJS.ArrayBufferView>( 2247 fd: number, 2248 buffer: TBuffer, 2249 offset: number | undefined | null, 2250 length: number | undefined | null, 2251 callback: (err: NodeJS.ErrnoException | null, written: number, buffer: TBuffer) => void, 2252 ): void; 2253 /** 2254 * Asynchronously writes `buffer` to the file referenced by the supplied file descriptor. 2255 * @param fd A file descriptor. 2256 * @param offset The part of the buffer to be written. If not supplied, defaults to `0`. 2257 */ 2258 export function write<TBuffer extends NodeJS.ArrayBufferView>( 2259 fd: number, 2260 buffer: TBuffer, 2261 offset: number | undefined | null, 2262 callback: (err: NodeJS.ErrnoException | null, written: number, buffer: TBuffer) => void, 2263 ): void; 2264 /** 2265 * Asynchronously writes `buffer` to the file referenced by the supplied file descriptor. 2266 * @param fd A file descriptor. 2267 */ 2268 export function write<TBuffer extends NodeJS.ArrayBufferView>( 2269 fd: number, 2270 buffer: TBuffer, 2271 callback: (err: NodeJS.ErrnoException | null, written: number, buffer: TBuffer) => void, 2272 ): void; 2273 /** 2274 * Asynchronously writes `string` to the file referenced by the supplied file descriptor. 2275 * @param fd A file descriptor. 2276 * @param string A string to write. 2277 * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position. 2278 * @param encoding The expected string encoding. 2279 */ 2280 export function write( 2281 fd: number, 2282 string: string, 2283 position: number | undefined | null, 2284 encoding: BufferEncoding | undefined | null, 2285 callback: (err: NodeJS.ErrnoException | null, written: number, str: string) => void, 2286 ): void; 2287 /** 2288 * Asynchronously writes `string` to the file referenced by the supplied file descriptor. 2289 * @param fd A file descriptor. 2290 * @param string A string to write. 2291 * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position. 2292 */ 2293 export function write( 2294 fd: number, 2295 string: string, 2296 position: number | undefined | null, 2297 callback: (err: NodeJS.ErrnoException | null, written: number, str: string) => void, 2298 ): void; 2299 /** 2300 * Asynchronously writes `string` to the file referenced by the supplied file descriptor. 2301 * @param fd A file descriptor. 2302 * @param string A string to write. 2303 */ 2304 export function write( 2305 fd: number, 2306 string: string, 2307 callback: (err: NodeJS.ErrnoException | null, written: number, str: string) => void, 2308 ): void; 2309 export namespace write { 2310 /** 2311 * Asynchronously writes `buffer` to the file referenced by the supplied file descriptor. 2312 * @param fd A file descriptor. 2313 * @param offset The part of the buffer to be written. If not supplied, defaults to `0`. 2314 * @param length The number of bytes to write. If not supplied, defaults to `buffer.length - offset`. 2315 * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position. 2316 */ 2317 function __promisify__<TBuffer extends NodeJS.ArrayBufferView>( 2318 fd: number, 2319 buffer?: TBuffer, 2320 offset?: number, 2321 length?: number, 2322 position?: number | null, 2323 ): Promise<{ 2324 bytesWritten: number; 2325 buffer: TBuffer; 2326 }>; 2327 /** 2328 * Asynchronously writes `string` to the file referenced by the supplied file descriptor. 2329 * @param fd A file descriptor. 2330 * @param string A string to write. 2331 * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position. 2332 * @param encoding The expected string encoding. 2333 */ 2334 function __promisify__( 2335 fd: number, 2336 string: string, 2337 position?: number | null, 2338 encoding?: BufferEncoding | null, 2339 ): Promise<{ 2340 bytesWritten: number; 2341 buffer: string; 2342 }>; 2343 } 2344 /** 2345 * If `buffer` is a plain object, it must have an own (not inherited) `toString`function property. 2346 * 2347 * For detailed information, see the documentation of the asynchronous version of 2348 * this API: {@link write}. 2349 * @since v0.1.21 2350 * @return The number of bytes written. 2351 */ 2352 export function writeSync( 2353 fd: number, 2354 buffer: NodeJS.ArrayBufferView, 2355 offset?: number | null, 2356 length?: number | null, 2357 position?: number | null, 2358 ): number; 2359 /** 2360 * Synchronously writes `string` to the file referenced by the supplied file descriptor, returning the number of bytes written. 2361 * @param fd A file descriptor. 2362 * @param string A string to write. 2363 * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position. 2364 * @param encoding The expected string encoding. 2365 */ 2366 export function writeSync( 2367 fd: number, 2368 string: string, 2369 position?: number | null, 2370 encoding?: BufferEncoding | null, 2371 ): number; 2372 export type ReadPosition = number | bigint; 2373 /** 2374 * Read data from the file specified by `fd`. 2375 * 2376 * The callback is given the three arguments, `(err, bytesRead, buffer)`. 2377 * 2378 * If the file is not modified concurrently, the end-of-file is reached when the 2379 * number of bytes read is zero. 2380 * 2381 * If this method is invoked as its `util.promisify()` ed version, it returns 2382 * a promise for an `Object` with `bytesRead` and `buffer` properties. 2383 * @since v0.0.2 2384 * @param buffer The buffer that the data will be written to. 2385 * @param offset The position in `buffer` to write the data to. 2386 * @param length The number of bytes to read. 2387 * @param position Specifies where to begin reading from in the file. If `position` is `null` or `-1 `, data will be read from the current file position, and the file position will be updated. If 2388 * `position` is an integer, the file position will be unchanged. 2389 */ 2390 export function read<TBuffer extends NodeJS.ArrayBufferView>( 2391 fd: number, 2392 buffer: TBuffer, 2393 offset: number, 2394 length: number, 2395 position: ReadPosition | null, 2396 callback: (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: TBuffer) => void, 2397 ): void; 2398 export namespace read { 2399 /** 2400 * @param fd A file descriptor. 2401 * @param buffer The buffer that the data will be written to. 2402 * @param offset The offset in the buffer at which to start writing. 2403 * @param length The number of bytes to read. 2404 * @param position The offset from the beginning of the file from which data should be read. If `null`, data will be read from the current position. 2405 */ 2406 function __promisify__<TBuffer extends NodeJS.ArrayBufferView>( 2407 fd: number, 2408 buffer: TBuffer, 2409 offset: number, 2410 length: number, 2411 position: number | null, 2412 ): Promise<{ 2413 bytesRead: number; 2414 buffer: TBuffer; 2415 }>; 2416 } 2417 export interface ReadSyncOptions { 2418 /** 2419 * @default 0 2420 */ 2421 offset?: number | undefined; 2422 /** 2423 * @default `length of buffer` 2424 */ 2425 length?: number | undefined; 2426 /** 2427 * @default null 2428 */ 2429 position?: ReadPosition | null | undefined; 2430 } 2431 /** 2432 * Returns the number of `bytesRead`. 2433 * 2434 * For detailed information, see the documentation of the asynchronous version of 2435 * this API: {@link read}. 2436 * @since v0.1.21 2437 */ 2438 export function readSync( 2439 fd: number, 2440 buffer: NodeJS.ArrayBufferView, 2441 offset: number, 2442 length: number, 2443 position: ReadPosition | null, 2444 ): number; 2445 /** 2446 * Similar to the above `fs.readSync` function, this version takes an optional `options` object. 2447 * If no `options` object is specified, it will default with the above values. 2448 */ 2449 export function readSync(fd: number, buffer: NodeJS.ArrayBufferView, opts?: ReadSyncOptions): number; 2450 /** 2451 * Asynchronously reads the entire contents of a file. 2452 * 2453 * ```js 2454 * import { readFile } from 'fs'; 2455 * 2456 * readFile('/etc/passwd', (err, data) => { 2457 * if (err) throw err; 2458 * console.log(data); 2459 * }); 2460 * ``` 2461 * 2462 * The callback is passed two arguments `(err, data)`, where `data` is the 2463 * contents of the file. 2464 * 2465 * If no encoding is specified, then the raw buffer is returned. 2466 * 2467 * If `options` is a string, then it specifies the encoding: 2468 * 2469 * ```js 2470 * import { readFile } from 'fs'; 2471 * 2472 * readFile('/etc/passwd', 'utf8', callback); 2473 * ``` 2474 * 2475 * When the path is a directory, the behavior of `fs.readFile()` and {@link readFileSync} is platform-specific. On macOS, Linux, and Windows, an 2476 * error will be returned. On FreeBSD, a representation of the directory's contents 2477 * will be returned. 2478 * 2479 * ```js 2480 * import { readFile } from 'fs'; 2481 * 2482 * // macOS, Linux, and Windows 2483 * readFile('<directory>', (err, data) => { 2484 * // => [Error: EISDIR: illegal operation on a directory, read <directory>] 2485 * }); 2486 * 2487 * // FreeBSD 2488 * readFile('<directory>', (err, data) => { 2489 * // => null, <data> 2490 * }); 2491 * ``` 2492 * 2493 * It is possible to abort an ongoing request using an `AbortSignal`. If a 2494 * request is aborted the callback is called with an `AbortError`: 2495 * 2496 * ```js 2497 * import { readFile } from 'fs'; 2498 * 2499 * const controller = new AbortController(); 2500 * const signal = controller.signal; 2501 * readFile(fileInfo[0].name, { signal }, (err, buf) => { 2502 * // ... 2503 * }); 2504 * // When you want to abort the request 2505 * controller.abort(); 2506 * ``` 2507 * 2508 * The `fs.readFile()` function buffers the entire file. To minimize memory costs, 2509 * when possible prefer streaming via `fs.createReadStream()`. 2510 * 2511 * Aborting an ongoing request does not abort individual operating 2512 * system requests but rather the internal buffering `fs.readFile` performs. 2513 * @since v0.1.29 2514 * @param path filename or file descriptor 2515 */ 2516 export function readFile( 2517 path: PathOrFileDescriptor, 2518 options: 2519 | ({ 2520 encoding?: null | undefined; 2521 flag?: string | undefined; 2522 } & Abortable) 2523 | undefined 2524 | null, 2525 callback: (err: NodeJS.ErrnoException | null, data: Buffer) => void, 2526 ): void; 2527 /** 2528 * Asynchronously reads the entire contents of a file. 2529 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 2530 * If a file descriptor is provided, the underlying file will _not_ be closed automatically. 2531 * @param options Either the encoding for the result, or an object that contains the encoding and an optional flag. 2532 * If a flag is not provided, it defaults to `'r'`. 2533 */ 2534 export function readFile( 2535 path: PathOrFileDescriptor, 2536 options: 2537 | ({ 2538 encoding: BufferEncoding; 2539 flag?: string | undefined; 2540 } & Abortable) 2541 | BufferEncoding, 2542 callback: (err: NodeJS.ErrnoException | null, data: string) => void, 2543 ): void; 2544 /** 2545 * Asynchronously reads the entire contents of a file. 2546 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 2547 * If a file descriptor is provided, the underlying file will _not_ be closed automatically. 2548 * @param options Either the encoding for the result, or an object that contains the encoding and an optional flag. 2549 * If a flag is not provided, it defaults to `'r'`. 2550 */ 2551 export function readFile( 2552 path: PathOrFileDescriptor, 2553 options: 2554 | (ObjectEncodingOptions & { 2555 flag?: string | undefined; 2556 } & Abortable) 2557 | BufferEncoding 2558 | undefined 2559 | null, 2560 callback: (err: NodeJS.ErrnoException | null, data: string | Buffer) => void, 2561 ): void; 2562 /** 2563 * Asynchronously reads the entire contents of a file. 2564 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 2565 * If a file descriptor is provided, the underlying file will _not_ be closed automatically. 2566 */ 2567 export function readFile( 2568 path: PathOrFileDescriptor, 2569 callback: (err: NodeJS.ErrnoException | null, data: Buffer) => void, 2570 ): void; 2571 export namespace readFile { 2572 /** 2573 * Asynchronously reads the entire contents of a file. 2574 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 2575 * If a file descriptor is provided, the underlying file will _not_ be closed automatically. 2576 * @param options An object that may contain an optional flag. 2577 * If a flag is not provided, it defaults to `'r'`. 2578 */ 2579 function __promisify__( 2580 path: PathOrFileDescriptor, 2581 options?: { 2582 encoding?: null | undefined; 2583 flag?: string | undefined; 2584 } | null, 2585 ): Promise<Buffer>; 2586 /** 2587 * Asynchronously reads the entire contents of a file. 2588 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 2589 * URL support is _experimental_. 2590 * If a file descriptor is provided, the underlying file will _not_ be closed automatically. 2591 * @param options Either the encoding for the result, or an object that contains the encoding and an optional flag. 2592 * If a flag is not provided, it defaults to `'r'`. 2593 */ 2594 function __promisify__( 2595 path: PathOrFileDescriptor, 2596 options: 2597 | { 2598 encoding: BufferEncoding; 2599 flag?: string | undefined; 2600 } 2601 | BufferEncoding, 2602 ): Promise<string>; 2603 /** 2604 * Asynchronously reads the entire contents of a file. 2605 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 2606 * URL support is _experimental_. 2607 * If a file descriptor is provided, the underlying file will _not_ be closed automatically. 2608 * @param options Either the encoding for the result, or an object that contains the encoding and an optional flag. 2609 * If a flag is not provided, it defaults to `'r'`. 2610 */ 2611 function __promisify__( 2612 path: PathOrFileDescriptor, 2613 options?: 2614 | (ObjectEncodingOptions & { 2615 flag?: string | undefined; 2616 }) 2617 | BufferEncoding 2618 | null, 2619 ): Promise<string | Buffer>; 2620 } 2621 /** 2622 * Returns the contents of the `path`. 2623 * 2624 * For detailed information, see the documentation of the asynchronous version of 2625 * this API: {@link readFile}. 2626 * 2627 * If the `encoding` option is specified then this function returns a 2628 * string. Otherwise it returns a buffer. 2629 * 2630 * Similar to {@link readFile}, when the path is a directory, the behavior of`fs.readFileSync()` is platform-specific. 2631 * 2632 * ```js 2633 * import { readFileSync } from 'fs'; 2634 * 2635 * // macOS, Linux, and Windows 2636 * readFileSync('<directory>'); 2637 * // => [Error: EISDIR: illegal operation on a directory, read <directory>] 2638 * 2639 * // FreeBSD 2640 * readFileSync('<directory>'); // => <data> 2641 * ``` 2642 * @since v0.1.8 2643 * @param path filename or file descriptor 2644 */ 2645 export function readFileSync( 2646 path: PathOrFileDescriptor, 2647 options?: { 2648 encoding?: null | undefined; 2649 flag?: string | undefined; 2650 } | null, 2651 ): Buffer; 2652 /** 2653 * Synchronously reads the entire contents of a file. 2654 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 2655 * If a file descriptor is provided, the underlying file will _not_ be closed automatically. 2656 * @param options Either the encoding for the result, or an object that contains the encoding and an optional flag. 2657 * If a flag is not provided, it defaults to `'r'`. 2658 */ 2659 export function readFileSync( 2660 path: PathOrFileDescriptor, 2661 options: 2662 | { 2663 encoding: BufferEncoding; 2664 flag?: string | undefined; 2665 } 2666 | BufferEncoding, 2667 ): string; 2668 /** 2669 * Synchronously reads the entire contents of a file. 2670 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 2671 * If a file descriptor is provided, the underlying file will _not_ be closed automatically. 2672 * @param options Either the encoding for the result, or an object that contains the encoding and an optional flag. 2673 * If a flag is not provided, it defaults to `'r'`. 2674 */ 2675 export function readFileSync( 2676 path: PathOrFileDescriptor, 2677 options?: 2678 | (ObjectEncodingOptions & { 2679 flag?: string | undefined; 2680 }) 2681 | BufferEncoding 2682 | null, 2683 ): string | Buffer; 2684 export type WriteFileOptions = 2685 | ( 2686 & ObjectEncodingOptions 2687 & Abortable 2688 & { 2689 mode?: Mode | undefined; 2690 flag?: string | undefined; 2691 } 2692 ) 2693 | BufferEncoding 2694 | null; 2695 /** 2696 * When `file` is a filename, asynchronously writes data to the file, replacing the 2697 * file if it already exists. `data` can be a string or a buffer. 2698 * 2699 * When `file` is a file descriptor, the behavior is similar to calling`fs.write()` directly (which is recommended). See the notes below on using 2700 * a file descriptor. 2701 * 2702 * The `encoding` option is ignored if `data` is a buffer. 2703 * 2704 * The `mode` option only affects the newly created file. See {@link open} for more details. 2705 * 2706 * If `data` is a plain object, it must have an own (not inherited) `toString`function property. 2707 * 2708 * ```js 2709 * import { writeFile } from 'fs'; 2710 * import { Buffer } from 'buffer'; 2711 * 2712 * const data = new Uint8Array(Buffer.from('Hello Node.js')); 2713 * writeFile('message.txt', data, (err) => { 2714 * if (err) throw err; 2715 * console.log('The file has been saved!'); 2716 * }); 2717 * ``` 2718 * 2719 * If `options` is a string, then it specifies the encoding: 2720 * 2721 * ```js 2722 * import { writeFile } from 'fs'; 2723 * 2724 * writeFile('message.txt', 'Hello Node.js', 'utf8', callback); 2725 * ``` 2726 * 2727 * It is unsafe to use `fs.writeFile()` multiple times on the same file without 2728 * waiting for the callback. For this scenario, {@link createWriteStream} is 2729 * recommended. 2730 * 2731 * Similarly to `fs.readFile` \- `fs.writeFile` is a convenience method that 2732 * performs multiple `write` calls internally to write the buffer passed to it. 2733 * For performance sensitive code consider using {@link createWriteStream}. 2734 * 2735 * It is possible to use an `AbortSignal` to cancel an `fs.writeFile()`. 2736 * Cancelation is "best effort", and some amount of data is likely still 2737 * to be written. 2738 * 2739 * ```js 2740 * import { writeFile } from 'fs'; 2741 * import { Buffer } from 'buffer'; 2742 * 2743 * const controller = new AbortController(); 2744 * const { signal } = controller; 2745 * const data = new Uint8Array(Buffer.from('Hello Node.js')); 2746 * writeFile('message.txt', data, { signal }, (err) => { 2747 * // When a request is aborted - the callback is called with an AbortError 2748 * }); 2749 * // When the request should be aborted 2750 * controller.abort(); 2751 * ``` 2752 * 2753 * Aborting an ongoing request does not abort individual operating 2754 * system requests but rather the internal buffering `fs.writeFile` performs. 2755 * @since v0.1.29 2756 * @param file filename or file descriptor 2757 */ 2758 export function writeFile( 2759 file: PathOrFileDescriptor, 2760 data: string | NodeJS.ArrayBufferView, 2761 options: WriteFileOptions, 2762 callback: NoParamCallback, 2763 ): void; 2764 /** 2765 * Asynchronously writes data to a file, replacing the file if it already exists. 2766 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 2767 * If a file descriptor is provided, the underlying file will _not_ be closed automatically. 2768 * @param data The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string. 2769 */ 2770 export function writeFile( 2771 path: PathOrFileDescriptor, 2772 data: string | NodeJS.ArrayBufferView, 2773 callback: NoParamCallback, 2774 ): void; 2775 export namespace writeFile { 2776 /** 2777 * Asynchronously writes data to a file, replacing the file if it already exists. 2778 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. 2779 * URL support is _experimental_. 2780 * If a file descriptor is provided, the underlying file will _not_ be closed automatically. 2781 * @param data The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string. 2782 * @param options Either the encoding for the file, or an object optionally specifying the encoding, file mode, and flag. 2783 * If `encoding` is not supplied, the default of `'utf8'` is used. 2784 * If `mode` is not supplied, the default of `0o666` is used. 2785 * If `mode` is a string, it is parsed as an octal integer. 2786 * If `flag` is not supplied, the default of `'w'` is used. 2787 */ 2788 function __promisify__( 2789 path: PathOrFileDescriptor, 2790 data: string | NodeJS.ArrayBufferView, 2791 options?: WriteFileOptions, 2792 ): Promise<void>; 2793 } 2794 /** 2795 * Returns `undefined`. 2796 * 2797 * If `data` is a plain object, it must have an own (not inherited) `toString`function property. 2798 * 2799 * The `mode` option only affects the newly created file. See {@link open} for more details. 2800 * 2801 * For detailed information, see the documentation of the asynchronous version of 2802 * this API: {@link writeFile}. 2803 * @since v0.1.29 2804 * @param file filename or file descriptor 2805 */ 2806 export function writeFileSync( 2807 file: PathOrFileDescriptor, 2808 data: string | NodeJS.ArrayBufferView, 2809 options?: WriteFileOptions, 2810 ): void; 2811 /** 2812 * Asynchronously append data to a file, creating the file if it does not yet 2813 * exist. `data` can be a string or a `Buffer`. 2814 * 2815 * The `mode` option only affects the newly created file. See {@link open} for more details. 2816 * 2817 * ```js 2818 * import { appendFile } from 'fs'; 2819 * 2820 * appendFile('message.txt', 'data to append', (err) => { 2821 * if (err) throw err; 2822 * console.log('The "data to append" was appended to file!'); 2823 * }); 2824 * ``` 2825 * 2826 * If `options` is a string, then it specifies the encoding: 2827 * 2828 * ```js 2829 * import { appendFile } from 'fs'; 2830 * 2831 * appendFile('message.txt', 'data to append', 'utf8', callback); 2832 * ``` 2833 * 2834 * The `path` may be specified as a numeric file descriptor that has been opened 2835 * for appending (using `fs.open()` or `fs.openSync()`). The file descriptor will 2836 * not be closed automatically. 2837 * 2838 * ```js 2839 * import { open, close, appendFile } from 'fs'; 2840 * 2841 * function closeFd(fd) { 2842 * close(fd, (err) => { 2843 * if (err) throw err; 2844 * }); 2845 * } 2846 * 2847 * open('message.txt', 'a', (err, fd) => { 2848 * if (err) throw err; 2849 * 2850 * try { 2851 * appendFile(fd, 'data to append', 'utf8', (err) => { 2852 * closeFd(fd); 2853 * if (err) throw err; 2854 * }); 2855 * } catch (err) { 2856 * closeFd(fd); 2857 * throw err; 2858 * } 2859 * }); 2860 * ``` 2861 * @since v0.6.7 2862 * @param path filename or file descriptor 2863 */ 2864 export function appendFile( 2865 path: PathOrFileDescriptor, 2866 data: string | Uint8Array, 2867 options: WriteFileOptions, 2868 callback: NoParamCallback, 2869 ): void; 2870 /** 2871 * Asynchronously append data to a file, creating the file if it does not exist. 2872 * @param file A path to a file. If a URL is provided, it must use the `file:` protocol. 2873 * If a file descriptor is provided, the underlying file will _not_ be closed automatically. 2874 * @param data The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string. 2875 */ 2876 export function appendFile(file: PathOrFileDescriptor, data: string | Uint8Array, callback: NoParamCallback): void; 2877 export namespace appendFile { 2878 /** 2879 * Asynchronously append data to a file, creating the file if it does not exist. 2880 * @param file A path to a file. If a URL is provided, it must use the `file:` protocol. 2881 * URL support is _experimental_. 2882 * If a file descriptor is provided, the underlying file will _not_ be closed automatically. 2883 * @param data The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string. 2884 * @param options Either the encoding for the file, or an object optionally specifying the encoding, file mode, and flag. 2885 * If `encoding` is not supplied, the default of `'utf8'` is used. 2886 * If `mode` is not supplied, the default of `0o666` is used. 2887 * If `mode` is a string, it is parsed as an octal integer. 2888 * If `flag` is not supplied, the default of `'a'` is used. 2889 */ 2890 function __promisify__( 2891 file: PathOrFileDescriptor, 2892 data: string | Uint8Array, 2893 options?: WriteFileOptions, 2894 ): Promise<void>; 2895 } 2896 /** 2897 * Synchronously append data to a file, creating the file if it does not yet 2898 * exist. `data` can be a string or a `Buffer`. 2899 * 2900 * The `mode` option only affects the newly created file. See {@link open} for more details. 2901 * 2902 * ```js 2903 * import { appendFileSync } from 'fs'; 2904 * 2905 * try { 2906 * appendFileSync('message.txt', 'data to append'); 2907 * console.log('The "data to append" was appended to file!'); 2908 * } catch (err) { 2909 * // Handle the error 2910 * } 2911 * ``` 2912 * 2913 * If `options` is a string, then it specifies the encoding: 2914 * 2915 * ```js 2916 * import { appendFileSync } from 'fs'; 2917 * 2918 * appendFileSync('message.txt', 'data to append', 'utf8'); 2919 * ``` 2920 * 2921 * The `path` may be specified as a numeric file descriptor that has been opened 2922 * for appending (using `fs.open()` or `fs.openSync()`). The file descriptor will 2923 * not be closed automatically. 2924 * 2925 * ```js 2926 * import { openSync, closeSync, appendFileSync } from 'fs'; 2927 * 2928 * let fd; 2929 * 2930 * try { 2931 * fd = openSync('message.txt', 'a'); 2932 * appendFileSync(fd, 'data to append', 'utf8'); 2933 * } catch (err) { 2934 * // Handle the error 2935 * } finally { 2936 * if (fd !== undefined) 2937 * closeSync(fd); 2938 * } 2939 * ``` 2940 * @since v0.6.7 2941 * @param path filename or file descriptor 2942 */ 2943 export function appendFileSync( 2944 path: PathOrFileDescriptor, 2945 data: string | Uint8Array, 2946 options?: WriteFileOptions, 2947 ): void; 2948 /** 2949 * Watch for changes on `filename`. The callback `listener` will be called each 2950 * time the file is accessed. 2951 * 2952 * The `options` argument may be omitted. If provided, it should be an object. The`options` object may contain a boolean named `persistent` that indicates 2953 * whether the process should continue to run as long as files are being watched. 2954 * The `options` object may specify an `interval` property indicating how often the 2955 * target should be polled in milliseconds. 2956 * 2957 * The `listener` gets two arguments the current stat object and the previous 2958 * stat object: 2959 * 2960 * ```js 2961 * import { watchFile } from 'fs'; 2962 * 2963 * watchFile('message.text', (curr, prev) => { 2964 * console.log(`the current mtime is: ${curr.mtime}`); 2965 * console.log(`the previous mtime was: ${prev.mtime}`); 2966 * }); 2967 * ``` 2968 * 2969 * These stat objects are instances of `fs.Stat`. If the `bigint` option is `true`, 2970 * the numeric values in these objects are specified as `BigInt`s. 2971 * 2972 * To be notified when the file was modified, not just accessed, it is necessary 2973 * to compare `curr.mtimeMs` and `prev.mtimeMs`. 2974 * 2975 * When an `fs.watchFile` operation results in an `ENOENT` error, it 2976 * will invoke the listener once, with all the fields zeroed (or, for dates, the 2977 * Unix Epoch). If the file is created later on, the listener will be called 2978 * again, with the latest stat objects. This is a change in functionality since 2979 * v0.10. 2980 * 2981 * Using {@link watch} is more efficient than `fs.watchFile` and `fs.unwatchFile`. `fs.watch` should be used instead of `fs.watchFile` and `fs.unwatchFile` when possible. 2982 * 2983 * When a file being watched by `fs.watchFile()` disappears and reappears, 2984 * then the contents of `previous` in the second callback event (the file's 2985 * reappearance) will be the same as the contents of `previous` in the first 2986 * callback event (its disappearance). 2987 * 2988 * This happens when: 2989 * 2990 * * the file is deleted, followed by a restore 2991 * * the file is renamed and then renamed a second time back to its original name 2992 * @since v0.1.31 2993 */ 2994 export interface WatchFileOptions { 2995 bigint?: boolean | undefined; 2996 persistent?: boolean | undefined; 2997 interval?: number | undefined; 2998 } 2999 export function watchFile( 3000 filename: PathLike, 3001 options: 3002 | (WatchFileOptions & { 3003 bigint?: false | undefined; 3004 }) 3005 | undefined, 3006 listener: StatsListener, 3007 ): StatWatcher; 3008 export function watchFile( 3009 filename: PathLike, 3010 options: 3011 | (WatchFileOptions & { 3012 bigint: true; 3013 }) 3014 | undefined, 3015 listener: BigIntStatsListener, 3016 ): StatWatcher; 3017 /** 3018 * Watch for changes on `filename`. The callback `listener` will be called each time the file is accessed. 3019 * @param filename A path to a file or directory. If a URL is provided, it must use the `file:` protocol. 3020 * @param listener The callback listener will be called each time the file is accessed. 3021 */ 3022 export function watchFile(filename: PathLike, listener: StatsListener): StatWatcher; 3023 /** 3024 * Stop watching for changes on `filename`. If `listener` is specified, only that 3025 * particular listener is removed. Otherwise, _all_ listeners are removed, 3026 * effectively stopping watching of `filename`. 3027 * 3028 * Calling `fs.unwatchFile()` with a filename that is not being watched is a 3029 * no-op, not an error. 3030 * 3031 * Using {@link watch} is more efficient than `fs.watchFile()` and `fs.unwatchFile()`. `fs.watch()` should be used instead of `fs.watchFile()` and `fs.unwatchFile()` when possible. 3032 * @since v0.1.31 3033 * @param listener Optional, a listener previously attached using `fs.watchFile()` 3034 */ 3035 export function unwatchFile(filename: PathLike, listener?: StatsListener): void; 3036 export function unwatchFile(filename: PathLike, listener?: BigIntStatsListener): void; 3037 export interface WatchOptions extends Abortable { 3038 encoding?: BufferEncoding | "buffer" | undefined; 3039 persistent?: boolean | undefined; 3040 recursive?: boolean | undefined; 3041 } 3042 export type WatchEventType = "rename" | "change"; 3043 export type WatchListener<T> = (event: WatchEventType, filename: T | null) => void; 3044 export type StatsListener = (curr: Stats, prev: Stats) => void; 3045 export type BigIntStatsListener = (curr: BigIntStats, prev: BigIntStats) => void; 3046 /** 3047 * Watch for changes on `filename`, where `filename` is either a file or a 3048 * directory. 3049 * 3050 * The second argument is optional. If `options` is provided as a string, it 3051 * specifies the `encoding`. Otherwise `options` should be passed as an object. 3052 * 3053 * The listener callback gets two arguments `(eventType, filename)`. `eventType`is either `'rename'` or `'change'`, and `filename` is the name of the file 3054 * which triggered the event. 3055 * 3056 * On most platforms, `'rename'` is emitted whenever a filename appears or 3057 * disappears in the directory. 3058 * 3059 * The listener callback is attached to the `'change'` event fired by `fs.FSWatcher`, but it is not the same thing as the `'change'` value of`eventType`. 3060 * 3061 * If a `signal` is passed, aborting the corresponding AbortController will close 3062 * the returned `fs.FSWatcher`. 3063 * @since v0.5.10 3064 * @param listener 3065 */ 3066 export function watch( 3067 filename: PathLike, 3068 options: 3069 | (WatchOptions & { 3070 encoding: "buffer"; 3071 }) 3072 | "buffer", 3073 listener?: WatchListener<Buffer>, 3074 ): FSWatcher; 3075 /** 3076 * Watch for changes on `filename`, where `filename` is either a file or a directory, returning an `FSWatcher`. 3077 * @param filename A path to a file or directory. If a URL is provided, it must use the `file:` protocol. 3078 * @param options Either the encoding for the filename provided to the listener, or an object optionally specifying encoding, persistent, and recursive options. 3079 * If `encoding` is not supplied, the default of `'utf8'` is used. 3080 * If `persistent` is not supplied, the default of `true` is used. 3081 * If `recursive` is not supplied, the default of `false` is used. 3082 */ 3083 export function watch( 3084 filename: PathLike, 3085 options?: WatchOptions | BufferEncoding | null, 3086 listener?: WatchListener<string>, 3087 ): FSWatcher; 3088 /** 3089 * Watch for changes on `filename`, where `filename` is either a file or a directory, returning an `FSWatcher`. 3090 * @param filename A path to a file or directory. If a URL is provided, it must use the `file:` protocol. 3091 * @param options Either the encoding for the filename provided to the listener, or an object optionally specifying encoding, persistent, and recursive options. 3092 * If `encoding` is not supplied, the default of `'utf8'` is used. 3093 * If `persistent` is not supplied, the default of `true` is used. 3094 * If `recursive` is not supplied, the default of `false` is used. 3095 */ 3096 export function watch( 3097 filename: PathLike, 3098 options: WatchOptions | string, 3099 listener?: WatchListener<string | Buffer>, 3100 ): FSWatcher; 3101 /** 3102 * Watch for changes on `filename`, where `filename` is either a file or a directory, returning an `FSWatcher`. 3103 * @param filename A path to a file or directory. If a URL is provided, it must use the `file:` protocol. 3104 */ 3105 export function watch(filename: PathLike, listener?: WatchListener<string>): FSWatcher; 3106 /** 3107 * Test whether or not the given path exists by checking with the file system. 3108 * Then call the `callback` argument with either true or false: 3109 * 3110 * ```js 3111 * import { exists } from 'fs'; 3112 * 3113 * exists('/etc/passwd', (e) => { 3114 * console.log(e ? 'it exists' : 'no passwd!'); 3115 * }); 3116 * ``` 3117 * 3118 * **The parameters for this callback are not consistent with other Node.js** 3119 * **callbacks.** Normally, the first parameter to a Node.js callback is an `err`parameter, optionally followed by other parameters. The `fs.exists()` callback 3120 * has only one boolean parameter. This is one reason `fs.access()` is recommended 3121 * instead of `fs.exists()`. 3122 * 3123 * Using `fs.exists()` to check for the existence of a file before calling`fs.open()`, `fs.readFile()` or `fs.writeFile()` is not recommended. Doing 3124 * so introduces a race condition, since other processes may change the file's 3125 * state between the two calls. Instead, user code should open/read/write the 3126 * file directly and handle the error raised if the file does not exist. 3127 * 3128 * **write (NOT RECOMMENDED)** 3129 * 3130 * ```js 3131 * import { exists, open, close } from 'fs'; 3132 * 3133 * exists('myfile', (e) => { 3134 * if (e) { 3135 * console.error('myfile already exists'); 3136 * } else { 3137 * open('myfile', 'wx', (err, fd) => { 3138 * if (err) throw err; 3139 * 3140 * try { 3141 * writeMyData(fd); 3142 * } finally { 3143 * close(fd, (err) => { 3144 * if (err) throw err; 3145 * }); 3146 * } 3147 * }); 3148 * } 3149 * }); 3150 * ``` 3151 * 3152 * **write (RECOMMENDED)** 3153 * 3154 * ```js 3155 * import { open, close } from 'fs'; 3156 * open('myfile', 'wx', (err, fd) => { 3157 * if (err) { 3158 * if (err.code === 'EEXIST') { 3159 * console.error('myfile already exists'); 3160 * return; 3161 * } 3162 * 3163 * throw err; 3164 * } 3165 * 3166 * try { 3167 * writeMyData(fd); 3168 * } finally { 3169 * close(fd, (err) => { 3170 * if (err) throw err; 3171 * }); 3172 * } 3173 * }); 3174 * ``` 3175 * 3176 * **read (NOT RECOMMENDED)** 3177 * 3178 * ```js 3179 * import { open, close, exists } from 'fs'; 3180 * 3181 * exists('myfile', (e) => { 3182 * if (e) { 3183 * open('myfile', 'r', (err, fd) => { 3184 * if (err) throw err; 3185 * 3186 * try { 3187 * readMyData(fd); 3188 * } finally { 3189 * close(fd, (err) => { 3190 * if (err) throw err; 3191 * }); 3192 * } 3193 * }); 3194 * } else { 3195 * console.error('myfile does not exist'); 3196 * } 3197 * }); 3198 * ``` 3199 * 3200 * **read (RECOMMENDED)** 3201 * 3202 * ```js 3203 * import { open, close } from 'fs'; 3204 * 3205 * open('myfile', 'r', (err, fd) => { 3206 * if (err) { 3207 * if (err.code === 'ENOENT') { 3208 * console.error('myfile does not exist'); 3209 * return; 3210 * } 3211 * 3212 * throw err; 3213 * } 3214 * 3215 * try { 3216 * readMyData(fd); 3217 * } finally { 3218 * close(fd, (err) => { 3219 * if (err) throw err; 3220 * }); 3221 * } 3222 * }); 3223 * ``` 3224 * 3225 * The "not recommended" examples above check for existence and then use the 3226 * file; the "recommended" examples are better because they use the file directly 3227 * and handle the error, if any. 3228 * 3229 * In general, check for the existence of a file only if the file won’t be 3230 * used directly, for example when its existence is a signal from another 3231 * process. 3232 * @since v0.0.2 3233 * @deprecated Since v1.0.0 - Use {@link stat} or {@link access} instead. 3234 */ 3235 export function exists(path: PathLike, callback: (exists: boolean) => void): void; 3236 /** @deprecated */ 3237 export namespace exists { 3238 /** 3239 * @param path A path to a file or directory. If a URL is provided, it must use the `file:` protocol. 3240 * URL support is _experimental_. 3241 */ 3242 function __promisify__(path: PathLike): Promise<boolean>; 3243 } 3244 /** 3245 * Returns `true` if the path exists, `false` otherwise. 3246 * 3247 * For detailed information, see the documentation of the asynchronous version of 3248 * this API: {@link exists}. 3249 * 3250 * `fs.exists()` is deprecated, but `fs.existsSync()` is not. The `callback`parameter to `fs.exists()` accepts parameters that are inconsistent with other 3251 * Node.js callbacks. `fs.existsSync()` does not use a callback. 3252 * 3253 * ```js 3254 * import { existsSync } from 'fs'; 3255 * 3256 * if (existsSync('/etc/passwd')) 3257 * console.log('The path exists.'); 3258 * ``` 3259 * @since v0.1.21 3260 */ 3261 export function existsSync(path: PathLike): boolean; 3262 export namespace constants { 3263 // File Access Constants 3264 /** Constant for fs.access(). File is visible to the calling process. */ 3265 const F_OK: number; 3266 /** Constant for fs.access(). File can be read by the calling process. */ 3267 const R_OK: number; 3268 /** Constant for fs.access(). File can be written by the calling process. */ 3269 const W_OK: number; 3270 /** Constant for fs.access(). File can be executed by the calling process. */ 3271 const X_OK: number; 3272 // File Copy Constants 3273 /** Constant for fs.copyFile. Flag indicating the destination file should not be overwritten if it already exists. */ 3274 const COPYFILE_EXCL: number; 3275 /** 3276 * Constant for fs.copyFile. copy operation will attempt to create a copy-on-write reflink. 3277 * If the underlying platform does not support copy-on-write, then a fallback copy mechanism is used. 3278 */ 3279 const COPYFILE_FICLONE: number; 3280 /** 3281 * Constant for fs.copyFile. Copy operation will attempt to create a copy-on-write reflink. 3282 * If the underlying platform does not support copy-on-write, then the operation will fail with an error. 3283 */ 3284 const COPYFILE_FICLONE_FORCE: number; 3285 // File Open Constants 3286 /** Constant for fs.open(). Flag indicating to open a file for read-only access. */ 3287 const O_RDONLY: number; 3288 /** Constant for fs.open(). Flag indicating to open a file for write-only access. */ 3289 const O_WRONLY: number; 3290 /** Constant for fs.open(). Flag indicating to open a file for read-write access. */ 3291 const O_RDWR: number; 3292 /** Constant for fs.open(). Flag indicating to create the file if it does not already exist. */ 3293 const O_CREAT: number; 3294 /** Constant for fs.open(). Flag indicating that opening a file should fail if the O_CREAT flag is set and the file already exists. */ 3295 const O_EXCL: number; 3296 /** 3297 * Constant for fs.open(). Flag indicating that if path identifies a terminal device, 3298 * opening the path shall not cause that terminal to become the controlling terminal for the process 3299 * (if the process does not already have one). 3300 */ 3301 const O_NOCTTY: number; 3302 /** Constant for fs.open(). Flag indicating that if the file exists and is a regular file, and the file is opened successfully for write access, its length shall be truncated to zero. */ 3303 const O_TRUNC: number; 3304 /** Constant for fs.open(). Flag indicating that data will be appended to the end of the file. */ 3305 const O_APPEND: number; 3306 /** Constant for fs.open(). Flag indicating that the open should fail if the path is not a directory. */ 3307 const O_DIRECTORY: number; 3308 /** 3309 * constant for fs.open(). 3310 * Flag indicating reading accesses to the file system will no longer result in 3311 * an update to the atime information associated with the file. 3312 * This flag is available on Linux operating systems only. 3313 */ 3314 const O_NOATIME: number; 3315 /** Constant for fs.open(). Flag indicating that the open should fail if the path is a symbolic link. */ 3316 const O_NOFOLLOW: number; 3317 /** Constant for fs.open(). Flag indicating that the file is opened for synchronous I/O. */ 3318 const O_SYNC: number; 3319 /** Constant for fs.open(). Flag indicating that the file is opened for synchronous I/O with write operations waiting for data integrity. */ 3320 const O_DSYNC: number; 3321 /** Constant for fs.open(). Flag indicating to open the symbolic link itself rather than the resource it is pointing to. */ 3322 const O_SYMLINK: number; 3323 /** Constant for fs.open(). When set, an attempt will be made to minimize caching effects of file I/O. */ 3324 const O_DIRECT: number; 3325 /** Constant for fs.open(). Flag indicating to open the file in nonblocking mode when possible. */ 3326 const O_NONBLOCK: number; 3327 // File Type Constants 3328 /** Constant for fs.Stats mode property for determining a file's type. Bit mask used to extract the file type code. */ 3329 const S_IFMT: number; 3330 /** Constant for fs.Stats mode property for determining a file's type. File type constant for a regular file. */ 3331 const S_IFREG: number; 3332 /** Constant for fs.Stats mode property for determining a file's type. File type constant for a directory. */ 3333 const S_IFDIR: number; 3334 /** Constant for fs.Stats mode property for determining a file's type. File type constant for a character-oriented device file. */ 3335 const S_IFCHR: number; 3336 /** Constant for fs.Stats mode property for determining a file's type. File type constant for a block-oriented device file. */ 3337 const S_IFBLK: number; 3338 /** Constant for fs.Stats mode property for determining a file's type. File type constant for a FIFO/pipe. */ 3339 const S_IFIFO: number; 3340 /** Constant for fs.Stats mode property for determining a file's type. File type constant for a symbolic link. */ 3341 const S_IFLNK: number; 3342 /** Constant for fs.Stats mode property for determining a file's type. File type constant for a socket. */ 3343 const S_IFSOCK: number; 3344 // File Mode Constants 3345 /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable, writable and executable by owner. */ 3346 const S_IRWXU: number; 3347 /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable by owner. */ 3348 const S_IRUSR: number; 3349 /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating writable by owner. */ 3350 const S_IWUSR: number; 3351 /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating executable by owner. */ 3352 const S_IXUSR: number; 3353 /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable, writable and executable by group. */ 3354 const S_IRWXG: number; 3355 /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable by group. */ 3356 const S_IRGRP: number; 3357 /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating writable by group. */ 3358 const S_IWGRP: number; 3359 /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating executable by group. */ 3360 const S_IXGRP: number; 3361 /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable, writable and executable by others. */ 3362 const S_IRWXO: number; 3363 /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable by others. */ 3364 const S_IROTH: number; 3365 /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating writable by others. */ 3366 const S_IWOTH: number; 3367 /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating executable by others. */ 3368 const S_IXOTH: number; 3369 /** 3370 * When set, a memory file mapping is used to access the file. This flag 3371 * is available on Windows operating systems only. On other operating systems, 3372 * this flag is ignored. 3373 */ 3374 const UV_FS_O_FILEMAP: number; 3375 } 3376 /** 3377 * Tests a user's permissions for the file or directory specified by `path`. 3378 * The `mode` argument is an optional integer that specifies the accessibility 3379 * checks to be performed. Check `File access constants` for possible values 3380 * of `mode`. It is possible to create a mask consisting of the bitwise OR of 3381 * two or more values (e.g. `fs.constants.W_OK | fs.constants.R_OK`). 3382 * 3383 * The final argument, `callback`, is a callback function that is invoked with 3384 * a possible error argument. If any of the accessibility checks fail, the error 3385 * argument will be an `Error` object. The following examples check if`package.json` exists, and if it is readable or writable. 3386 * 3387 * ```js 3388 * import { access, constants } from 'fs'; 3389 * 3390 * const file = 'package.json'; 3391 * 3392 * // Check if the file exists in the current directory. 3393 * access(file, constants.F_OK, (err) => { 3394 * console.log(`${file} ${err ? 'does not exist' : 'exists'}`); 3395 * }); 3396 * 3397 * // Check if the file is readable. 3398 * access(file, constants.R_OK, (err) => { 3399 * console.log(`${file} ${err ? 'is not readable' : 'is readable'}`); 3400 * }); 3401 * 3402 * // Check if the file is writable. 3403 * access(file, constants.W_OK, (err) => { 3404 * console.log(`${file} ${err ? 'is not writable' : 'is writable'}`); 3405 * }); 3406 * 3407 * // Check if the file exists in the current directory, and if it is writable. 3408 * access(file, constants.F_OK | constants.W_OK, (err) => { 3409 * if (err) { 3410 * console.error( 3411 * `${file} ${err.code === 'ENOENT' ? 'does not exist' : 'is read-only'}`); 3412 * } else { 3413 * console.log(`${file} exists, and it is writable`); 3414 * } 3415 * }); 3416 * ``` 3417 * 3418 * Do not use `fs.access()` to check for the accessibility of a file before calling`fs.open()`, `fs.readFile()` or `fs.writeFile()`. Doing 3419 * so introduces a race condition, since other processes may change the file's 3420 * state between the two calls. Instead, user code should open/read/write the 3421 * file directly and handle the error raised if the file is not accessible. 3422 * 3423 * **write (NOT RECOMMENDED)** 3424 * 3425 * ```js 3426 * import { access, open, close } from 'fs'; 3427 * 3428 * access('myfile', (err) => { 3429 * if (!err) { 3430 * console.error('myfile already exists'); 3431 * return; 3432 * } 3433 * 3434 * open('myfile', 'wx', (err, fd) => { 3435 * if (err) throw err; 3436 * 3437 * try { 3438 * writeMyData(fd); 3439 * } finally { 3440 * close(fd, (err) => { 3441 * if (err) throw err; 3442 * }); 3443 * } 3444 * }); 3445 * }); 3446 * ``` 3447 * 3448 * **write (RECOMMENDED)** 3449 * 3450 * ```js 3451 * import { open, close } from 'fs'; 3452 * 3453 * open('myfile', 'wx', (err, fd) => { 3454 * if (err) { 3455 * if (err.code === 'EEXIST') { 3456 * console.error('myfile already exists'); 3457 * return; 3458 * } 3459 * 3460 * throw err; 3461 * } 3462 * 3463 * try { 3464 * writeMyData(fd); 3465 * } finally { 3466 * close(fd, (err) => { 3467 * if (err) throw err; 3468 * }); 3469 * } 3470 * }); 3471 * ``` 3472 * 3473 * **read (NOT RECOMMENDED)** 3474 * 3475 * ```js 3476 * import { access, open, close } from 'fs'; 3477 * access('myfile', (err) => { 3478 * if (err) { 3479 * if (err.code === 'ENOENT') { 3480 * console.error('myfile does not exist'); 3481 * return; 3482 * } 3483 * 3484 * throw err; 3485 * } 3486 * 3487 * open('myfile', 'r', (err, fd) => { 3488 * if (err) throw err; 3489 * 3490 * try { 3491 * readMyData(fd); 3492 * } finally { 3493 * close(fd, (err) => { 3494 * if (err) throw err; 3495 * }); 3496 * } 3497 * }); 3498 * }); 3499 * ``` 3500 * 3501 * **read (RECOMMENDED)** 3502 * 3503 * ```js 3504 * import { open, close } from 'fs'; 3505 * 3506 * open('myfile', 'r', (err, fd) => { 3507 * if (err) { 3508 * if (err.code === 'ENOENT') { 3509 * console.error('myfile does not exist'); 3510 * return; 3511 * } 3512 * 3513 * throw err; 3514 * } 3515 * 3516 * try { 3517 * readMyData(fd); 3518 * } finally { 3519 * close(fd, (err) => { 3520 * if (err) throw err; 3521 * }); 3522 * } 3523 * }); 3524 * ``` 3525 * 3526 * The "not recommended" examples above check for accessibility and then use the 3527 * file; the "recommended" examples are better because they use the file directly 3528 * and handle the error, if any. 3529 * 3530 * In general, check for the accessibility of a file only if the file will not be 3531 * used directly, for example when its accessibility is a signal from another 3532 * process. 3533 * 3534 * On Windows, access-control policies (ACLs) on a directory may limit access to 3535 * a file or directory. The `fs.access()` function, however, does not check the 3536 * ACL and therefore may report that a path is accessible even if the ACL restricts 3537 * the user from reading or writing to it. 3538 * @since v0.11.15 3539 * @param [mode=fs.constants.F_OK] 3540 */ 3541 export function access(path: PathLike, mode: number | undefined, callback: NoParamCallback): void; 3542 /** 3543 * Asynchronously tests a user's permissions for the file specified by path. 3544 * @param path A path to a file or directory. If a URL is provided, it must use the `file:` protocol. 3545 */ 3546 export function access(path: PathLike, callback: NoParamCallback): void; 3547 export namespace access { 3548 /** 3549 * Asynchronously tests a user's permissions for the file specified by path. 3550 * @param path A path to a file or directory. If a URL is provided, it must use the `file:` protocol. 3551 * URL support is _experimental_. 3552 */ 3553 function __promisify__(path: PathLike, mode?: number): Promise<void>; 3554 } 3555 /** 3556 * Synchronously tests a user's permissions for the file or directory specified 3557 * by `path`. The `mode` argument is an optional integer that specifies the 3558 * accessibility checks to be performed. Check `File access constants` for 3559 * possible values of `mode`. It is possible to create a mask consisting of 3560 * the bitwise OR of two or more values 3561 * (e.g. `fs.constants.W_OK | fs.constants.R_OK`). 3562 * 3563 * If any of the accessibility checks fail, an `Error` will be thrown. Otherwise, 3564 * the method will return `undefined`. 3565 * 3566 * ```js 3567 * import { accessSync, constants } from 'fs'; 3568 * 3569 * try { 3570 * accessSync('etc/passwd', constants.R_OK | constants.W_OK); 3571 * console.log('can read/write'); 3572 * } catch (err) { 3573 * console.error('no access!'); 3574 * } 3575 * ``` 3576 * @since v0.11.15 3577 * @param [mode=fs.constants.F_OK] 3578 */ 3579 export function accessSync(path: PathLike, mode?: number): void; 3580 interface StreamOptions { 3581 flags?: string | undefined; 3582 encoding?: BufferEncoding | undefined; 3583 fd?: number | promises.FileHandle | undefined; 3584 mode?: number | undefined; 3585 autoClose?: boolean | undefined; 3586 emitClose?: boolean | undefined; 3587 start?: number | undefined; 3588 highWaterMark?: number | undefined; 3589 } 3590 interface FSImplementation { 3591 open?: (...args: any[]) => any; 3592 close?: (...args: any[]) => any; 3593 } 3594 interface CreateReadStreamFSImplementation extends FSImplementation { 3595 read: (...args: any[]) => any; 3596 } 3597 interface CreateWriteStreamFSImplementation extends FSImplementation { 3598 write: (...args: any[]) => any; 3599 writev?: (...args: any[]) => any; 3600 } 3601 interface ReadStreamOptions extends StreamOptions { 3602 fs?: CreateReadStreamFSImplementation | null | undefined; 3603 end?: number | undefined; 3604 } 3605 interface WriteStreamOptions extends StreamOptions { 3606 fs?: CreateWriteStreamFSImplementation | null | undefined; 3607 } 3608 /** 3609 * Unlike the 16 kb default `highWaterMark` for a `stream.Readable`, the stream 3610 * returned by this method has a default `highWaterMark` of 64 kb. 3611 * 3612 * `options` can include `start` and `end` values to read a range of bytes from 3613 * the file instead of the entire file. Both `start` and `end` are inclusive and 3614 * start counting at 0, allowed values are in the 3615 * \[0, [`Number.MAX_SAFE_INTEGER`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)\] range. If `fd` is specified and `start` is 3616 * omitted or `undefined`, `fs.createReadStream()` reads sequentially from the 3617 * current file position. The `encoding` can be any one of those accepted by `Buffer`. 3618 * 3619 * If `fd` is specified, `ReadStream` will ignore the `path` argument and will use 3620 * the specified file descriptor. This means that no `'open'` event will be 3621 * emitted. `fd` should be blocking; non-blocking `fd`s should be passed to `net.Socket`. 3622 * 3623 * If `fd` points to a character device that only supports blocking reads 3624 * (such as keyboard or sound card), read operations do not finish until data is 3625 * available. This can prevent the process from exiting and the stream from 3626 * closing naturally. 3627 * 3628 * By default, the stream will emit a `'close'` event after it has been 3629 * destroyed. Set the `emitClose` option to `false` to change this behavior. 3630 * 3631 * By providing the `fs` option, it is possible to override the corresponding `fs`implementations for `open`, `read`, and `close`. When providing the `fs` option, 3632 * an override for `read` is required. If no `fd` is provided, an override for`open` is also required. If `autoClose` is `true`, an override for `close` is 3633 * also required. 3634 * 3635 * ```js 3636 * import { createReadStream } from 'fs'; 3637 * 3638 * // Create a stream from some character device. 3639 * const stream = createReadStream('/dev/input/event0'); 3640 * setTimeout(() => { 3641 * stream.close(); // This may not close the stream. 3642 * // Artificially marking end-of-stream, as if the underlying resource had 3643 * // indicated end-of-file by itself, allows the stream to close. 3644 * // This does not cancel pending read operations, and if there is such an 3645 * // operation, the process may still not be able to exit successfully 3646 * // until it finishes. 3647 * stream.push(null); 3648 * stream.read(0); 3649 * }, 100); 3650 * ``` 3651 * 3652 * If `autoClose` is false, then the file descriptor won't be closed, even if 3653 * there's an error. It is the application's responsibility to close it and make 3654 * sure there's no file descriptor leak. If `autoClose` is set to true (default 3655 * behavior), on `'error'` or `'end'` the file descriptor will be closed 3656 * automatically. 3657 * 3658 * `mode` sets the file mode (permission and sticky bits), but only if the 3659 * file was created. 3660 * 3661 * An example to read the last 10 bytes of a file which is 100 bytes long: 3662 * 3663 * ```js 3664 * import { createReadStream } from 'fs'; 3665 * 3666 * createReadStream('sample.txt', { start: 90, end: 99 }); 3667 * ``` 3668 * 3669 * If `options` is a string, then it specifies the encoding. 3670 * @since v0.1.31 3671 */ 3672 export function createReadStream(path: PathLike, options?: BufferEncoding | ReadStreamOptions): ReadStream; 3673 /** 3674 * `options` may also include a `start` option to allow writing data at some 3675 * position past the beginning of the file, allowed values are in the 3676 * \[0, [`Number.MAX_SAFE_INTEGER`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)\] range. Modifying a file rather than replacing 3677 * it may require the `flags` option to be set to `r+` rather than the default `w`. 3678 * The `encoding` can be any one of those accepted by `Buffer`. 3679 * 3680 * If `autoClose` is set to true (default behavior) on `'error'` or `'finish'` the file descriptor will be closed automatically. If `autoClose` is false, 3681 * then the file descriptor won't be closed, even if there's an error. 3682 * It is the application's responsibility to close it and make sure there's no 3683 * file descriptor leak. 3684 * 3685 * By default, the stream will emit a `'close'` event after it has been 3686 * destroyed. Set the `emitClose` option to `false` to change this behavior. 3687 * 3688 * By providing the `fs` option it is possible to override the corresponding `fs`implementations for `open`, `write`, `writev` and `close`. Overriding `write()`without `writev()` can reduce 3689 * performance as some optimizations (`_writev()`) 3690 * will be disabled. When providing the `fs` option, overrides for at least one of`write` and `writev` are required. If no `fd` option is supplied, an override 3691 * for `open` is also required. If `autoClose` is `true`, an override for `close` is also required. 3692 * 3693 * Like `fs.ReadStream`, if `fd` is specified, `fs.WriteStream` will ignore the`path` argument and will use the specified file descriptor. This means that no`'open'` event will be 3694 * emitted. `fd` should be blocking; non-blocking `fd`s 3695 * should be passed to `net.Socket`. 3696 * 3697 * If `options` is a string, then it specifies the encoding. 3698 * @since v0.1.31 3699 */ 3700 export function createWriteStream(path: PathLike, options?: BufferEncoding | WriteStreamOptions): WriteStream; 3701 /** 3702 * Forces all currently queued I/O operations associated with the file to the 3703 * operating system's synchronized I/O completion state. Refer to the POSIX [`fdatasync(2)`](http://man7.org/linux/man-pages/man2/fdatasync.2.html) documentation for details. No arguments other 3704 * than a possible 3705 * exception are given to the completion callback. 3706 * @since v0.1.96 3707 */ 3708 export function fdatasync(fd: number, callback: NoParamCallback): void; 3709 export namespace fdatasync { 3710 /** 3711 * Asynchronous fdatasync(2) - synchronize a file's in-core state with storage device. 3712 * @param fd A file descriptor. 3713 */ 3714 function __promisify__(fd: number): Promise<void>; 3715 } 3716 /** 3717 * Forces all currently queued I/O operations associated with the file to the 3718 * operating system's synchronized I/O completion state. Refer to the POSIX [`fdatasync(2)`](http://man7.org/linux/man-pages/man2/fdatasync.2.html) documentation for details. Returns `undefined`. 3719 * @since v0.1.96 3720 */ 3721 export function fdatasyncSync(fd: number): void; 3722 /** 3723 * Asynchronously copies `src` to `dest`. By default, `dest` is overwritten if it 3724 * already exists. No arguments other than a possible exception are given to the 3725 * callback function. Node.js makes no guarantees about the atomicity of the copy 3726 * operation. If an error occurs after the destination file has been opened for 3727 * writing, Node.js will attempt to remove the destination. 3728 * 3729 * `mode` is an optional integer that specifies the behavior 3730 * of the copy operation. It is possible to create a mask consisting of the bitwise 3731 * OR of two or more values (e.g.`fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE`). 3732 * 3733 * * `fs.constants.COPYFILE_EXCL`: The copy operation will fail if `dest` already 3734 * exists. 3735 * * `fs.constants.COPYFILE_FICLONE`: The copy operation will attempt to create a 3736 * copy-on-write reflink. If the platform does not support copy-on-write, then a 3737 * fallback copy mechanism is used. 3738 * * `fs.constants.COPYFILE_FICLONE_FORCE`: The copy operation will attempt to 3739 * create a copy-on-write reflink. If the platform does not support 3740 * copy-on-write, then the operation will fail. 3741 * 3742 * ```js 3743 * import { copyFile, constants } from 'fs'; 3744 * 3745 * function callback(err) { 3746 * if (err) throw err; 3747 * console.log('source.txt was copied to destination.txt'); 3748 * } 3749 * 3750 * // destination.txt will be created or overwritten by default. 3751 * copyFile('source.txt', 'destination.txt', callback); 3752 * 3753 * // By using COPYFILE_EXCL, the operation will fail if destination.txt exists. 3754 * copyFile('source.txt', 'destination.txt', constants.COPYFILE_EXCL, callback); 3755 * ``` 3756 * @since v8.5.0 3757 * @param src source filename to copy 3758 * @param dest destination filename of the copy operation 3759 * @param [mode=0] modifiers for copy operation. 3760 */ 3761 export function copyFile(src: PathLike, dest: PathLike, callback: NoParamCallback): void; 3762 export function copyFile(src: PathLike, dest: PathLike, mode: number, callback: NoParamCallback): void; 3763 export namespace copyFile { 3764 function __promisify__(src: PathLike, dst: PathLike, mode?: number): Promise<void>; 3765 } 3766 /** 3767 * Synchronously copies `src` to `dest`. By default, `dest` is overwritten if it 3768 * already exists. Returns `undefined`. Node.js makes no guarantees about the 3769 * atomicity of the copy operation. If an error occurs after the destination file 3770 * has been opened for writing, Node.js will attempt to remove the destination. 3771 * 3772 * `mode` is an optional integer that specifies the behavior 3773 * of the copy operation. It is possible to create a mask consisting of the bitwise 3774 * OR of two or more values (e.g.`fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE`). 3775 * 3776 * * `fs.constants.COPYFILE_EXCL`: The copy operation will fail if `dest` already 3777 * exists. 3778 * * `fs.constants.COPYFILE_FICLONE`: The copy operation will attempt to create a 3779 * copy-on-write reflink. If the platform does not support copy-on-write, then a 3780 * fallback copy mechanism is used. 3781 * * `fs.constants.COPYFILE_FICLONE_FORCE`: The copy operation will attempt to 3782 * create a copy-on-write reflink. If the platform does not support 3783 * copy-on-write, then the operation will fail. 3784 * 3785 * ```js 3786 * import { copyFileSync, constants } from 'fs'; 3787 * 3788 * // destination.txt will be created or overwritten by default. 3789 * copyFileSync('source.txt', 'destination.txt'); 3790 * console.log('source.txt was copied to destination.txt'); 3791 * 3792 * // By using COPYFILE_EXCL, the operation will fail if destination.txt exists. 3793 * copyFileSync('source.txt', 'destination.txt', constants.COPYFILE_EXCL); 3794 * ``` 3795 * @since v8.5.0 3796 * @param src source filename to copy 3797 * @param dest destination filename of the copy operation 3798 * @param [mode=0] modifiers for copy operation. 3799 */ 3800 export function copyFileSync(src: PathLike, dest: PathLike, mode?: number): void; 3801 /** 3802 * Write an array of `ArrayBufferView`s to the file specified by `fd` using`writev()`. 3803 * 3804 * `position` is the offset from the beginning of the file where this data 3805 * should be written. If `typeof position !== 'number'`, the data will be written 3806 * at the current position. 3807 * 3808 * The callback will be given three arguments: `err`, `bytesWritten`, and`buffers`. `bytesWritten` is how many bytes were written from `buffers`. 3809 * 3810 * If this method is `util.promisify()` ed, it returns a promise for an`Object` with `bytesWritten` and `buffers` properties. 3811 * 3812 * It is unsafe to use `fs.writev()` multiple times on the same file without 3813 * waiting for the callback. For this scenario, use {@link createWriteStream}. 3814 * 3815 * On Linux, positional writes don't work when the file is opened in append mode. 3816 * The kernel ignores the position argument and always appends the data to 3817 * the end of the file. 3818 * @since v12.9.0 3819 */ 3820 export function writev( 3821 fd: number, 3822 buffers: readonly NodeJS.ArrayBufferView[], 3823 cb: (err: NodeJS.ErrnoException | null, bytesWritten: number, buffers: NodeJS.ArrayBufferView[]) => void, 3824 ): void; 3825 export function writev( 3826 fd: number, 3827 buffers: readonly NodeJS.ArrayBufferView[], 3828 position: number, 3829 cb: (err: NodeJS.ErrnoException | null, bytesWritten: number, buffers: NodeJS.ArrayBufferView[]) => void, 3830 ): void; 3831 export interface WriteVResult { 3832 bytesWritten: number; 3833 buffers: NodeJS.ArrayBufferView[]; 3834 } 3835 export namespace writev { 3836 function __promisify__( 3837 fd: number, 3838 buffers: readonly NodeJS.ArrayBufferView[], 3839 position?: number, 3840 ): Promise<WriteVResult>; 3841 } 3842 /** 3843 * For detailed information, see the documentation of the asynchronous version of 3844 * this API: {@link writev}. 3845 * @since v12.9.0 3846 * @return The number of bytes written. 3847 */ 3848 export function writevSync(fd: number, buffers: readonly NodeJS.ArrayBufferView[], position?: number): number; 3849 /** 3850 * Read from a file specified by `fd` and write to an array of `ArrayBufferView`s 3851 * using `readv()`. 3852 * 3853 * `position` is the offset from the beginning of the file from where data 3854 * should be read. If `typeof position !== 'number'`, the data will be read 3855 * from the current position. 3856 * 3857 * The callback will be given three arguments: `err`, `bytesRead`, and`buffers`. `bytesRead` is how many bytes were read from the file. 3858 * 3859 * If this method is invoked as its `util.promisify()` ed version, it returns 3860 * a promise for an `Object` with `bytesRead` and `buffers` properties. 3861 * @since v13.13.0, v12.17.0 3862 */ 3863 export function readv( 3864 fd: number, 3865 buffers: readonly NodeJS.ArrayBufferView[], 3866 cb: (err: NodeJS.ErrnoException | null, bytesRead: number, buffers: NodeJS.ArrayBufferView[]) => void, 3867 ): void; 3868 export function readv( 3869 fd: number, 3870 buffers: readonly NodeJS.ArrayBufferView[], 3871 position: number, 3872 cb: (err: NodeJS.ErrnoException | null, bytesRead: number, buffers: NodeJS.ArrayBufferView[]) => void, 3873 ): void; 3874 export interface ReadVResult { 3875 bytesRead: number; 3876 buffers: NodeJS.ArrayBufferView[]; 3877 } 3878 export namespace readv { 3879 function __promisify__( 3880 fd: number, 3881 buffers: readonly NodeJS.ArrayBufferView[], 3882 position?: number, 3883 ): Promise<ReadVResult>; 3884 } 3885 /** 3886 * For detailed information, see the documentation of the asynchronous version of 3887 * this API: {@link readv}. 3888 * @since v13.13.0, v12.17.0 3889 * @return The number of bytes read. 3890 */ 3891 export function readvSync(fd: number, buffers: readonly NodeJS.ArrayBufferView[], position?: number): number; 3892 export interface OpenDirOptions { 3893 encoding?: BufferEncoding | undefined; 3894 /** 3895 * Number of directory entries that are buffered 3896 * internally when reading from the directory. Higher values lead to better 3897 * performance but higher memory usage. 3898 * @default 32 3899 */ 3900 bufferSize?: number | undefined; 3901 } 3902 /** 3903 * Synchronously open a directory. See [`opendir(3)`](http://man7.org/linux/man-pages/man3/opendir.3.html). 3904 * 3905 * Creates an `fs.Dir`, which contains all further functions for reading from 3906 * and cleaning up the directory. 3907 * 3908 * The `encoding` option sets the encoding for the `path` while opening the 3909 * directory and subsequent read operations. 3910 * @since v12.12.0 3911 */ 3912 export function opendirSync(path: PathLike, options?: OpenDirOptions): Dir; 3913 /** 3914 * Asynchronously open a directory. See the POSIX [`opendir(3)`](http://man7.org/linux/man-pages/man3/opendir.3.html) documentation for 3915 * more details. 3916 * 3917 * Creates an `fs.Dir`, which contains all further functions for reading from 3918 * and cleaning up the directory. 3919 * 3920 * The `encoding` option sets the encoding for the `path` while opening the 3921 * directory and subsequent read operations. 3922 * @since v12.12.0 3923 */ 3924 export function opendir(path: PathLike, cb: (err: NodeJS.ErrnoException | null, dir: Dir) => void): void; 3925 export function opendir( 3926 path: PathLike, 3927 options: OpenDirOptions, 3928 cb: (err: NodeJS.ErrnoException | null, dir: Dir) => void, 3929 ): void; 3930 export namespace opendir { 3931 function __promisify__(path: PathLike, options?: OpenDirOptions): Promise<Dir>; 3932 } 3933 export interface BigIntStats extends StatsBase<bigint> { 3934 atimeNs: bigint; 3935 mtimeNs: bigint; 3936 ctimeNs: bigint; 3937 birthtimeNs: bigint; 3938 } 3939 export interface BigIntOptions { 3940 bigint: true; 3941 } 3942 export interface StatOptions { 3943 bigint?: boolean | undefined; 3944 } 3945 export interface StatSyncOptions extends StatOptions { 3946 throwIfNoEntry?: boolean | undefined; 3947 } 3948 interface CopyOptionsBase { 3949 /** 3950 * Dereference symlinks 3951 * @default false 3952 */ 3953 dereference?: boolean; 3954 /** 3955 * When `force` is `false`, and the destination 3956 * exists, throw an error. 3957 * @default false 3958 */ 3959 errorOnExist?: boolean; 3960 /** 3961 * Overwrite existing file or directory. _The copy 3962 * operation will ignore errors if you set this to false and the destination 3963 * exists. Use the `errorOnExist` option to change this behavior. 3964 * @default true 3965 */ 3966 force?: boolean; 3967 /** 3968 * When `true` timestamps from `src` will 3969 * be preserved. 3970 * @default false 3971 */ 3972 preserveTimestamps?: boolean; 3973 /** 3974 * Copy directories recursively. 3975 * @default false 3976 */ 3977 recursive?: boolean; 3978 } 3979 export interface CopyOptions extends CopyOptionsBase { 3980 /** 3981 * Function to filter copied files/directories. Return 3982 * `true` to copy the item, `false` to ignore it. 3983 */ 3984 filter?(source: string, destination: string): boolean | Promise<boolean>; 3985 } 3986 export interface CopySyncOptions extends CopyOptionsBase { 3987 /** 3988 * Function to filter copied files/directories. Return 3989 * `true` to copy the item, `false` to ignore it. 3990 */ 3991 filter?(source: string, destination: string): boolean; 3992 } 3993 /** 3994 * Asynchronously copies the entire directory structure from `src` to `dest`, 3995 * including subdirectories and files. 3996 * 3997 * When copying a directory to another directory, globs are not supported and 3998 * behavior is similar to `cp dir1/ dir2/`. 3999 * @since v16.7.0 4000 * @experimental 4001 * @param src source path to copy. 4002 * @param dest destination path to copy to. 4003 */ 4004 export function cp( 4005 source: string | URL, 4006 destination: string | URL, 4007 callback: (err: NodeJS.ErrnoException | null) => void, 4008 ): void; 4009 export function cp( 4010 source: string | URL, 4011 destination: string | URL, 4012 opts: CopyOptions, 4013 callback: (err: NodeJS.ErrnoException | null) => void, 4014 ): void; 4015 /** 4016 * Synchronously copies the entire directory structure from `src` to `dest`, 4017 * including subdirectories and files. 4018 * 4019 * When copying a directory to another directory, globs are not supported and 4020 * behavior is similar to `cp dir1/ dir2/`. 4021 * @since v16.7.0 4022 * @experimental 4023 * @param src source path to copy. 4024 * @param dest destination path to copy to. 4025 */ 4026 export function cpSync(source: string | URL, destination: string | URL, opts?: CopySyncOptions): void; 4027 } 4028 declare module "node:fs" { 4029 export * from "fs"; 4030 }