diff options
author | Antoine du Hamel <duhamelantoine1995@gmail.com> | 2022-04-20 11:23:41 +0300 |
---|---|---|
committer | Michaël Zasso <targos@protonmail.com> | 2022-04-28 07:56:14 +0300 |
commit | 3d65a3b13e61f136dc09027aa12473c2b3003725 (patch) | |
tree | 012259a22a22fec5325afd386bbae62e3293a09f /doc/api/fs.md | |
parent | 46c880b99b4a74c3a7c99fdd824bf9ce22333987 (diff) |
doc: add `node:` prefix for all core modules
Some core modules can be loaded with or without the `node:` prefix.
Using the prefix disambiguates which specifiers refer to core modules.
This commit updates the docs to use the prefix everywhere a core module
is referenced.
PR-URL: https://github.com/nodejs/node/pull/42752
Fixes: https://github.com/nodejs/node/issues/38343
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Mestery <mestery@protonmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Akhil Marsonya <akhil.marsonya27@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Diffstat (limited to 'doc/api/fs.md')
-rw-r--r-- | doc/api/fs.md | 204 |
1 files changed, 102 insertions, 102 deletions
diff --git a/doc/api/fs.md b/doc/api/fs.md index d584d1f20a0..246b369f318 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -8,27 +8,27 @@ <!-- source_link=lib/fs.js --> -The `fs` module enables interacting with the file system in a +The `node:fs` module enables interacting with the file system in a way modeled on standard POSIX functions. To use the promise-based APIs: ```mjs -import * as fs from 'fs/promises'; +import * as fs from 'node:fs/promises'; ``` ```cjs -const fs = require('fs/promises'); +const fs = require('node:fs/promises'); ``` To use the callback and sync APIs: ```mjs -import * as fs from 'fs'; +import * as fs from 'node:fs'; ``` ```cjs -const fs = require('fs'); +const fs = require('node:fs'); ``` All file system operations have synchronous, callback, and promise-based @@ -40,7 +40,7 @@ Promise-based operations return a promise that is fulfilled when the asynchronous operation is complete. ```mjs -import { unlink } from 'fs/promises'; +import { unlink } from 'node:fs/promises'; try { await unlink('/tmp/hello'); @@ -51,7 +51,7 @@ try { ``` ```cjs -const { unlink } = require('fs/promises'); +const { unlink } = require('node:fs/promises'); (async function(path) { try { @@ -72,7 +72,7 @@ reserved for an exception. If the operation is completed successfully, then the first argument is `null` or `undefined`. ```mjs -import { unlink } from 'fs'; +import { unlink } from 'node:fs'; unlink('/tmp/hello', (err) => { if (err) throw err; @@ -81,7 +81,7 @@ unlink('/tmp/hello', (err) => { ``` ```cjs -const { unlink } = require('fs'); +const { unlink } = require('node:fs'); unlink('/tmp/hello', (err) => { if (err) throw err; @@ -89,7 +89,7 @@ unlink('/tmp/hello', (err) => { }); ``` -The callback-based versions of the `fs` module APIs are preferable over +The callback-based versions of the `node:fs` module APIs are preferable over the use of the promise APIs when maximal performance (both in terms of execution time and memory allocation) is required. @@ -100,7 +100,7 @@ execution until the operation is complete. Exceptions are thrown immediately and can be handled using `try…catch`, or can be allowed to bubble up. ```mjs -import { unlinkSync } from 'fs'; +import { unlinkSync } from 'node:fs'; try { unlinkSync('/tmp/hello'); @@ -111,7 +111,7 @@ try { ``` ```cjs -const { unlinkSync } = require('fs'); +const { unlinkSync } = require('node:fs'); try { unlinkSync('/tmp/hello'); @@ -128,7 +128,7 @@ added: v10.0.0 changes: - version: v14.0.0 pr-url: https://github.com/nodejs/node/pull/31553 - description: Exposed as `require('fs/promises')`. + description: Exposed as `require('node:fs/promises')`. - version: - v11.14.0 - v10.17.0 @@ -136,7 +136,7 @@ changes: description: This API is no longer experimental. - version: v10.1.0 pr-url: https://github.com/nodejs/node/pull/20504 - description: The API is accessible via `require('fs').promises` only. + description: The API is accessible via `require('node:fs').promises` only. --> The `fs/promises` API provides asynchronous file system methods that return @@ -237,7 +237,7 @@ Closes the file handle after waiting for any pending operation on the handle to complete. ```mjs -import { open } from 'fs/promises'; +import { open } from 'node:fs/promises'; let filehandle; try { @@ -282,7 +282,7 @@ By default, the stream will emit a `'close'` event after it has been destroyed. Set the `emitClose` option to `false` to change this behavior. ```mjs -import { open } from 'fs/promises'; +import { open } from 'node:fs/promises'; const fd = await open('/dev/input/event0'); // Create a stream from some character device. @@ -308,7 +308,7 @@ automatically. An example to read the last 10 bytes of a file which is 100 bytes long: ```mjs -import { open } from 'fs/promises'; +import { open } from 'node:fs/promises'; const fd = await open('sample.txt'); fd.createReadStream({ start: 90, end: 99 }); @@ -448,7 +448,7 @@ await file.close(); ```cjs const { open, -} = require('fs/promises'); +} = require('node:fs/promises'); (async () => { const file = await open('./some/file/to/read'); @@ -552,7 +552,7 @@ retained in the file. The following example retains only the first four bytes of the file: ```mjs -import { open } from 'fs/promises'; +import { open } from 'node:fs/promises'; let filehandle = null; try { @@ -743,8 +743,8 @@ with an {Error} object. The following example checks if the file `/etc/passwd` can be read and written by the current process. ```mjs -import { access } from 'fs/promises'; -import { constants } from 'fs'; +import { access } from 'node:fs/promises'; +import { constants } from 'node:fs'; try { await access('/etc/passwd', constants.R_OK | constants.W_OK); @@ -846,8 +846,8 @@ error occurs after the destination file has been opened for writing, an attempt will be made to remove the destination. ```mjs -import { constants } from 'fs'; -import { copyFile } from 'fs/promises'; +import { constants } from 'node:fs'; +import { copyFile } from 'node:fs/promises'; try { await copyFile('source.txt', 'destination.txt'); @@ -1037,7 +1037,7 @@ The optional `options` argument can be a string specifying an encoding, or an object with an `encoding` property specifying the character encoding to use. ```mjs -import { mkdtemp } from 'fs/promises'; +import { mkdtemp } from 'node:fs/promises'; try { await mkdtemp(path.join(os.tmpdir(), 'foo-')); @@ -1050,7 +1050,7 @@ The `fsPromises.mkdtemp()` method will append the six randomly selected characters directly to the `prefix` string. For instance, given a directory `/tmp`, if the intention is to create a temporary directory _within_ `/tmp`, the `prefix` must end with a trailing platform-specific path separator -(`require('path').sep`). +(`require('node:path').sep`). ### `fsPromises.open(path, flags[, mode])` @@ -1110,7 +1110,7 @@ directory and subsequent read operations. Example using async iteration: ```mjs -import { opendir } from 'fs/promises'; +import { opendir } from 'node:fs/promises'; try { const dir = await opendir('./'); @@ -1152,7 +1152,7 @@ If `options.withFileTypes` is set to `true`, the resolved array will contain {fs.Dirent} objects. ```mjs -import { readdir } from 'fs/promises'; +import { readdir } from 'node:fs/promises'; try { const files = await readdir(path); @@ -1199,7 +1199,7 @@ It is possible to abort an ongoing `readFile` using an {AbortSignal}. If a request is aborted the promise returned is rejected with an `AbortError`: ```mjs -import { readFile } from 'fs/promises'; +import { readFile } from 'node:fs/promises'; try { const controller = new AbortController(); @@ -1474,7 +1474,7 @@ Returns an async iterator that watches for changes on `filename`, where `filenam is either a file or a directory. ```js -const { watch } = require('fs/promises'); +const { watch } = require('node:fs/promises'); const ac = new AbortController(); const { signal } = ac; @@ -1554,8 +1554,8 @@ Cancelation is "best effort", and some amount of data is likely still to be written. ```mjs -import { writeFile } from 'fs/promises'; -import { Buffer } from 'buffer'; +import { writeFile } from 'node:fs/promises'; +import { Buffer } from 'node:buffer'; try { const controller = new AbortController(); @@ -1629,7 +1629,7 @@ argument will be an `Error` object. The following examples check if `package.json` exists, and if it is readable or writable. ```mjs -import { access, constants } from 'fs'; +import { access, constants } from 'node:fs'; const file = 'package.json'; @@ -1663,7 +1663,7 @@ file directly and handle the error raised if the file is not accessible. **write (NOT RECOMMENDED)** ```mjs -import { access, open, close } from 'fs'; +import { access, open, close } from 'node:fs'; access('myfile', (err) => { if (!err) { @@ -1688,7 +1688,7 @@ access('myfile', (err) => { **write (RECOMMENDED)** ```mjs -import { open, close } from 'fs'; +import { open, close } from 'node:fs'; open('myfile', 'wx', (err, fd) => { if (err) { @@ -1713,7 +1713,7 @@ open('myfile', 'wx', (err, fd) => { **read (NOT RECOMMENDED)** ```mjs -import { access, open, close } from 'fs'; +import { access, open, close } from 'node:fs'; access('myfile', (err) => { if (err) { if (err.code === 'ENOENT') { @@ -1741,7 +1741,7 @@ access('myfile', (err) => { **read (RECOMMENDED)** ```mjs -import { open, close } from 'fs'; +import { open, close } from 'node:fs'; open('myfile', 'r', (err, fd) => { if (err) { @@ -1818,7 +1818,7 @@ The `mode` option only affects the newly created file. See [`fs.open()`][] for more details. ```mjs -import { appendFile } from 'fs'; +import { appendFile } from 'node:fs'; appendFile('message.txt', 'data to append', (err) => { if (err) throw err; @@ -1829,7 +1829,7 @@ appendFile('message.txt', 'data to append', (err) => { If `options` is a string, then it specifies the encoding: ```mjs -import { appendFile } from 'fs'; +import { appendFile } from 'node:fs'; appendFile('message.txt', 'data to append', 'utf8', callback); ``` @@ -1839,7 +1839,7 @@ for appending (using `fs.open()` or `fs.openSync()`). The file descriptor will not be closed automatically. ```mjs -import { open, close, appendFile } from 'fs'; +import { open, close, appendFile } from 'node:fs'; function closeFd(fd) { close(fd, (err) => { @@ -1897,7 +1897,7 @@ possible exception are given to the completion callback. See the POSIX chmod(2) documentation for more detail. ```mjs -import { chmod } from 'fs'; +import { chmod } from 'node:fs'; chmod('my_file.txt', 0o775, (err) => { if (err) throw err; @@ -2069,7 +2069,7 @@ OR of two or more values (e.g. copy-on-write, then the operation will fail. ```mjs -import { copyFile, constants } from 'fs'; +import { copyFile, constants } from 'node:fs'; function callback(err) { if (err) throw err; @@ -2217,7 +2217,7 @@ an override for `read` is required. If no `fd` is provided, an override for also required. ```mjs -import { createReadStream } from 'fs'; +import { createReadStream } from 'node:fs'; // Create a stream from some character device. const stream = createReadStream('/dev/input/event0'); @@ -2245,7 +2245,7 @@ file was created. An example to read the last 10 bytes of a file which is 100 bytes long: ```mjs -import { createReadStream } from 'fs'; +import { createReadStream } from 'node:fs'; createReadStream('sample.txt', { start: 90, end: 99 }); ``` @@ -2364,7 +2364,7 @@ Test whether or not the given path exists by checking with the file system. Then call the `callback` argument with either true or false: ```mjs -import { exists } from 'fs'; +import { exists } from 'node:fs'; exists('/etc/passwd', (e) => { console.log(e ? 'it exists' : 'no passwd!'); @@ -2386,7 +2386,7 @@ file directly and handle the error raised if the file does not exist. **write (NOT RECOMMENDED)** ```mjs -import { exists, open, close } from 'fs'; +import { exists, open, close } from 'node:fs'; exists('myfile', (e) => { if (e) { @@ -2410,7 +2410,7 @@ exists('myfile', (e) => { **write (RECOMMENDED)** ```mjs -import { open, close } from 'fs'; +import { open, close } from 'node:fs'; open('myfile', 'wx', (err, fd) => { if (err) { if (err.code === 'EEXIST') { @@ -2434,7 +2434,7 @@ open('myfile', 'wx', (err, fd) => { **read (NOT RECOMMENDED)** ```mjs -import { open, close, exists } from 'fs'; +import { open, close, exists } from 'node:fs'; exists('myfile', (e) => { if (e) { @@ -2458,7 +2458,7 @@ exists('myfile', (e) => { **read (RECOMMENDED)** ```mjs -import { open, close } from 'fs'; +import { open, close } from 'node:fs'; open('myfile', 'r', (err, fd) => { if (err) { @@ -2680,7 +2680,7 @@ For example, the following program retains only the first four bytes of the file: ```mjs -import { open, close, ftruncate } from 'fs'; +import { open, close, ftruncate } from 'node:fs'; function closeFd(fd) { close(fd, (err) => { @@ -2972,7 +2972,7 @@ property indicating whether parent directories should be created. Calling when `recursive` is false. ```mjs -import { mkdir } from 'fs'; +import { mkdir } from 'node:fs'; // Creates /tmp/a/apple, regardless of whether `/tmp` and /tmp/a exist. mkdir('/tmp/a/apple', { recursive: true }, (err) => { @@ -2984,7 +2984,7 @@ On Windows, using `fs.mkdir()` on the root directory even with recursion will result in an error: ```mjs -import { mkdir } from 'fs'; +import { mkdir } from 'node:fs'; mkdir('/', { recursive: true }, (err) => { // => [Error: EPERM: operation not permitted, mkdir 'C:\'] @@ -3043,7 +3043,7 @@ The optional `options` argument can be a string specifying an encoding, or an object with an `encoding` property specifying the character encoding to use. ```mjs -import { mkdtemp } from 'fs'; +import { mkdtemp } from 'node:fs'; mkdtemp(path.join(os.tmpdir(), 'foo-'), (err, directory) => { if (err) throw err; @@ -3056,11 +3056,11 @@ The `fs.mkdtemp()` method will append the six randomly selected characters directly to the `prefix` string. For instance, given a directory `/tmp`, if the intention is to create a temporary directory _within_ `/tmp`, the `prefix` must end with a trailing platform-specific path separator -(`require('path').sep`). +(`require('node:path').sep`). ```mjs -import { tmpdir } from 'os'; -import { mkdtemp } from 'fs'; +import { tmpdir } from 'node:os'; +import { mkdtemp } from 'node:fs'; // The parent directory for the new temporary directory const tmpDir = tmpdir(); @@ -3075,7 +3075,7 @@ mkdtemp(tmpDir, (err, directory) => { }); // This method is *CORRECT*: -import { sep } from 'path'; +import { sep } from 'node:path'; mkdtemp(`${tmpDir}${sep}`, (err, directory) => { if (err) throw err; console.log(directory); @@ -3348,7 +3348,7 @@ changes: Asynchronously reads the entire contents of a file. ```mjs -import { readFile } from 'fs'; +import { readFile } from 'node:fs'; readFile('/etc/passwd', (err, data) => { if (err) throw err; @@ -3364,7 +3364,7 @@ If no encoding is specified, then the raw buffer is returned. If `options` is a string, then it specifies the encoding: ```mjs -import { readFile } from 'fs'; +import { readFile } from 'node:fs'; readFile('/etc/passwd', 'utf8', callback); ``` @@ -3375,7 +3375,7 @@ error will be returned. On FreeBSD, a representation of the directory's contents will be returned. ```mjs -import { readFile } from 'fs'; +import { readFile } from 'node:fs'; // macOS, Linux, and Windows readFile('<directory>', (err, data) => { @@ -3392,7 +3392,7 @@ It is possible to abort an ongoing request using an `AbortSignal`. If a request is aborted the callback is called with an `AbortError`: ```mjs -import { readFile } from 'fs'; +import { readFile } from 'node:fs'; const controller = new AbortController(); const signal = controller.signal; @@ -3657,7 +3657,7 @@ given to the completion callback. See also: rename(2). ```mjs -import { rename } from 'fs'; +import { rename } from 'node:fs'; rename('oldFile.txt', 'newFile.txt', (err) => { if (err) throw err; @@ -3838,7 +3838,7 @@ For example, given the following directory structure: The next program will check for the stats of the given paths: ```mjs -import { stat } from 'fs'; +import { stat } from 'node:fs'; const pathsToCheck = ['./txtDir', './txtDir/file.txt']; @@ -3939,7 +3939,7 @@ require the destination path to be absolute. When using `'junction'`, the Relative targets are relative to the link’s parent directory. ```mjs -import { symlink } from 'fs'; +import { symlink } from 'node:fs'; symlink('./mew', './mewtwo', callback); ``` @@ -3988,7 +3988,7 @@ given to the completion callback. A file descriptor can also be passed as the first argument. In this case, `fs.ftruncate()` is called. ```mjs -import { truncate } from 'fs'; +import { truncate } from 'node:fs'; // Assuming that 'path/file.txt' is a regular file. truncate('path/file.txt', (err) => { if (err) throw err; @@ -3997,7 +3997,7 @@ truncate('path/file.txt', (err) => { ``` ```cjs -const { truncate } = require('fs'); +const { truncate } = require('node:fs'); // Assuming that 'path/file.txt' is a regular file. truncate('path/file.txt', (err) => { if (err) throw err; @@ -4042,7 +4042,7 @@ Asynchronously removes a file or symbolic link. No arguments other than a possible exception are given to the completion callback. ```mjs -import { unlink } from 'fs'; +import { unlink } from 'node:fs'; // Assuming that 'path/file.txt' is a regular file. unlink('path/file.txt', (err) => { if (err) throw err; @@ -4241,7 +4241,7 @@ guaranteed to be provided. Therefore, don't assume that `filename` argument is always provided in the callback, and have some fallback logic if it is `null`. ```mjs -import { watch } from 'fs'; +import { watch } from 'node:fs'; watch('somedir', (eventType, filename) => { console.log(`event type is: ${eventType}`); if (filename) { @@ -4289,7 +4289,7 @@ The `listener` gets two arguments the current stat object and the previous stat object: ```mjs -import { watchFile } from 'fs'; +import { watchFile } from 'node:fs'; watchFile('message.text', (curr, prev) => { console.log(`the current mtime is: ${curr.mtime}`); @@ -4533,8 +4533,8 @@ The `mode` option only affects the newly created file. See [`fs.open()`][] for more details. ```mjs -import { writeFile } from 'fs'; -import { Buffer } from 'buffer'; +import { writeFile } from 'node:fs'; +import { Buffer } from 'node:buffer'; const data = new Uint8Array(Buffer.from('Hello Node.js')); writeFile('message.txt', data, (err) => { @@ -4546,7 +4546,7 @@ writeFile('message.txt', data, (err) => { If `options` is a string, then it specifies the encoding: ```mjs -import { writeFile } from 'fs'; +import { writeFile } from 'node:fs'; writeFile('message.txt', 'Hello Node.js', 'utf8', callback); ``` @@ -4564,8 +4564,8 @@ Cancelation is "best effort", and some amount of data is likely still to be written. ```mjs -import { writeFile } from 'fs'; -import { Buffer } from 'buffer'; +import { writeFile } from 'node:fs'; +import { Buffer } from 'node:buffer'; const controller = new AbortController(); const { signal } = controller; @@ -4586,8 +4586,8 @@ When `file` is a file descriptor, the behavior is almost identical to directly calling `fs.write()` like: ```mjs -import { write } from 'fs'; -import { Buffer } from 'buffer'; +import { write } from 'node:fs'; +import { Buffer } from 'node:buffer'; write(fd, Buffer.from(data, options.encoding), callback); ``` @@ -4680,7 +4680,7 @@ If any of the accessibility checks fail, an `Error` will be thrown. Otherwise, the method will return `undefined`. ```mjs -import { accessSync, constants } from 'fs'; +import { accessSync, constants } from 'node:fs'; try { accessSync('etc/passwd', constants.R_OK | constants.W_OK); @@ -4717,7 +4717,7 @@ The `mode` option only affects the newly created file. See [`fs.open()`][] for more details. ```mjs -import { appendFileSync } from 'fs'; +import { appendFileSync } from 'node:fs'; try { appendFileSync('message.txt', 'data to append'); @@ -4730,7 +4730,7 @@ try { If `options` is a string, then it specifies the encoding: ```mjs -import { appendFileSync } from 'fs'; +import { appendFileSync } from 'node:fs'; appendFileSync('message.txt', 'data to append', 'utf8'); ``` @@ -4740,7 +4740,7 @@ for appending (using `fs.open()` or `fs.openSync()`). The file descriptor will not be closed automatically. ```mjs -import { openSync, closeSync, appendFileSync } from 'fs'; +import { openSync, closeSync, appendFileSync } from 'node:fs'; let fd; @@ -4844,7 +4844,7 @@ OR of two or more values (e.g. copy-on-write, then the operation will fail. ```mjs -import { copyFileSync, constants } from 'fs'; +import { copyFileSync, constants } from 'node:fs'; // destination.txt will be created or overwritten by default. copyFileSync('source.txt', 'destination.txt'); @@ -4915,7 +4915,7 @@ parameter to `fs.exists()` accepts parameters that are inconsistent with other Node.js callbacks. `fs.existsSync()` does not use a callback. ```mjs -import { existsSync } from 'fs'; +import { existsSync } from 'node:fs'; if (existsSync('/etc/passwd')) console.log('The path exists.'); @@ -5303,7 +5303,7 @@ Similar to [`fs.readFile()`][], when the path is a directory, the behavior of `fs.readFileSync()` is platform-specific. ```mjs -import { readFileSync } from 'fs'; +import { readFileSync } from 'node:fs'; // macOS, Linux, and Windows readFileSync('<directory>'); @@ -5823,7 +5823,7 @@ Created by [`fs.opendir()`][], [`fs.opendirSync()`][], or [`fsPromises.opendir()`][]. ```mjs -import { opendir } from 'fs/promises'; +import { opendir } from 'node:fs/promises'; try { const dir = await opendir('./'); @@ -6100,7 +6100,7 @@ support. If `filename` is provided, it will be provided as a {Buffer} if `filename` will be a UTF-8 string. ```mjs -import { watch } from 'fs'; +import { watch } from 'node:fs'; // Example when handled through fs.watch() listener watch('./tmp', { encoding: 'buffer' }, (eventType, filename) => { if (filename) { @@ -6778,7 +6778,7 @@ To use more than one constant, use the bitwise OR `|` operator. Example: ```mjs -import { open, constants } from 'fs'; +import { open, constants } from 'node:fs'; const { O_RDWR, @@ -7076,7 +7076,7 @@ It is important to correctly order the operations by awaiting the results of one before invoking the other: ```mjs -import { rename, stat } from 'fs/promises'; +import { rename, stat } from 'node:fs/promises'; const from = '/tmp/hello'; const to = '/tmp/world'; @@ -7091,7 +7091,7 @@ try { ``` ```cjs -const { rename, stat } = require('fs/promises'); +const { rename, stat } = require('node:fs/promises'); (async function(from, to) { try { @@ -7108,7 +7108,7 @@ Or, when using the callback APIs, move the `fs.stat()` call into the callback of the `fs.rename()` operation: ```mjs -import { rename, stat } from 'fs'; +import { rename, stat } from 'node:fs'; rename('/tmp/hello', '/tmp/world', (err) => { if (err) throw err; @@ -7120,7 +7120,7 @@ rename('/tmp/hello', '/tmp/world', (err) => { ``` ```cjs -const { rename, stat } = require('fs/promises'); +const { rename, stat } = require('node:fs/promises'); rename('/tmp/hello', '/tmp/world', (err) => { if (err) throw err; @@ -7145,7 +7145,7 @@ to the current working directory as determined by calling `process.cwd()`. Example using an absolute path on POSIX: ```mjs -import { open } from 'fs/promises'; +import { open } from 'node:fs/promises'; let fd; try { @@ -7159,7 +7159,7 @@ try { Example using a relative path on POSIX (relative to `process.cwd()`): ```mjs -import { open } from 'fs/promises'; +import { open } from 'node:fs/promises'; let fd; try { @@ -7176,11 +7176,11 @@ try { added: v7.6.0 --> -For most `fs` module functions, the `path` or `filename` argument may be passed -as a {URL} object using the `file:` protocol. +For most `node:fs` module functions, the `path` or `filename` argument may be +passed as a {URL} object using the `file:` protocol. ```mjs -import { readFileSync } from 'fs'; +import { readFileSync } from 'node:fs'; readFileSync(new URL('file:///tmp/hello')); ``` @@ -7194,7 +7194,7 @@ On Windows, `file:` {URL}s with a host name convert to UNC paths, while `file:` with no host name and no drive letter will result in an error: ```mjs -import { readFileSync } from 'fs'; +import { readFileSync } from 'node:fs'; // On Windows : // - WHATWG file URLs with hostname convert to UNC path @@ -7218,7 +7218,7 @@ On all other platforms, `file:` {URL}s with a host name are unsupported and will result in an error: ```mjs -import { readFileSync } from 'fs'; +import { readFileSync } from 'node:fs'; // On other platforms: // - WHATWG file URLs with hostname are unsupported @@ -7235,7 +7235,7 @@ A `file:` {URL} having encoded slash characters will result in an error on all platforms: ```mjs -import { readFileSync } from 'fs'; +import { readFileSync } from 'node:fs'; // On Windows readFileSync(new URL('file:///C:/p/a/t/h/%2F')); @@ -7253,7 +7253,7 @@ readFileSync(new URL('file:///p/a/t/h/%2f')); On Windows, `file:` {URL}s having encoded backslash will result in an error: ```mjs -import { readFileSync } from 'fs'; +import { readFileSync } from 'node:fs'; // On Windows readFileSync(new URL('file:///C:/path/%5C')); @@ -7273,8 +7273,8 @@ be relative or absolute: Example using an absolute path on POSIX: ```mjs -import { open } from 'fs/promises'; -import { Buffer } from 'buffer'; +import { open } from 'node:fs/promises'; +import { Buffer } from 'node:buffer'; let fd; try { @@ -7314,7 +7314,7 @@ are completed. Failure to do so will result in a memory leak that will eventually cause an application to crash. ```mjs -import { open, close, fstat } from 'fs'; +import { open, close, fstat } from 'node:fs'; function closeFd(fd) { close(fd, (err) => { @@ -7348,7 +7348,7 @@ that resources are not leaked. However, it is still required that they are closed when operations are completed: ```mjs -import { open } from 'fs/promises'; +import { open } from 'node:fs/promises'; let file; try { |