Skip to content

nodeProtocols

Prefer the node: protocol prefix for Node.js built-in modules for clarity and consistency.

✅ This rule is included in the node logical presets.

The node: protocol makes it explicit that the module is a Node.js built-in, preventing confusion with npm packages that might have the same name. The protocol is supported in Node.js 16+ and is the recommended way to import built-ins.

import
module "fs"

The node:fs module enables interacting with the file system in a way modeled on standard POSIX functions.

To use the promise-based APIs:

import * as fs from 'node:fs/promises';

To use the callback and sync APIs:

import * as fs from 'node:fs';

All file system operations have synchronous, callback, and promise-based forms, and are accessible using both CommonJS syntax and ES6 Modules (ESM).

@seesource

fs
from "fs";
import
const path: path.PlatformPath
path
from "path";
import {
function readFile(path: fs.PathOrFileDescriptor, options: ({
encoding?: null | undefined;
flag?: string | undefined;
} & EventEmitter<T extends EventMap<T> = DefaultEventMap>.Abortable) | undefined | null, callback: (err: NodeJS.ErrnoException | null, data: NonSharedBuffer) => void): void (+3 overloads)

Asynchronously reads the entire contents of a file.

import { readFile } from 'node:fs';
readFile('/etc/passwd', (err, data) => {
if (err) throw err;
console.log(data);
});

The callback is passed two arguments (err, data), where data is the contents of the file.

If no encoding is specified, then the raw buffer is returned.

If options is a string, then it specifies the encoding:

import { readFile } from 'node:fs';
readFile('/etc/passwd', 'utf8', callback);

When the path is a directory, the behavior of fs.readFile() and

readFileSync

is platform-specific. On macOS, Linux, and Windows, an error will be returned. On FreeBSD, a representation of the directory's contents will be returned.

import { readFile } from 'node:fs';
// macOS, Linux, and Windows
readFile('<directory>', (err, data) => {
// => [Error: EISDIR: illegal operation on a directory, read <directory>]
});
// FreeBSD
readFile('<directory>', (err, data) => {
// => null, <data>
});

It is possible to abort an ongoing request using an AbortSignal. If a request is aborted the callback is called with an AbortError:

import { readFile } from 'node:fs';
const controller = new AbortController();
const signal = controller.signal;
readFile(fileInfo[0].name, { signal }, (err, buf) => {
// ...
});
// When you want to abort the request
controller.abort();

The fs.readFile() function buffers the entire file. To minimize memory costs, when possible prefer streaming via fs.createReadStream().

Aborting an ongoing request does not abort individual operating system requests but rather the internal buffering fs.readFile performs.

@sincev0.1.29

@parampath filename or file descriptor

readFile
} from "fs";
import
module "fs/promises"

The fs/promises API provides asynchronous file system methods that return promises.

The promise APIs use the underlying Node.js threadpool to perform file system operations off the event loop thread. These operations are not synchronized or threadsafe. Care must be taken when performing multiple concurrent modifications on the same file or data corruption may occur.

@sincev10.0.0

fsPromises
from "fs/promises";
const
const crypto: any
crypto
=
var require: NodeJS.Require
(id: string) => any

Used to import modules, JSON, and local files.

@sincev0.1.13

require
("crypto");

This rule is not configurable.

If you need to support Node.js versions older than 16, you should not use this rule, as the node: protocol was introduced in Node.js 16.0.0.

If you strongly prefer more succinct imports and aren’t worried about confusing userland modules with built-in Node.js modules, this rule might not be for you.

Made with ❤️‍🔥 around the world by the Flint team and contributors.