Skip to content

noSyncMethods (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noSyncMethods": "error"
}
}
}
}

Disallow calling synchronous methods, such as fs.readFileSync().

Many Node.js functions come in two versions. For example, fs.readFile() reads a file in the background and lets the program keep running, while fs.readFileSync() stops everything else until the file is read. By convention, the names of the blocking versions end with Sync.

Blocking calls are fine in a command-line script that does one thing at a time. In a web server, a single blocking call holds up every other request until it finishes.

This rule reports every call to a function or method whose name ends with Sync, including calls made through .call() and .apply(). It only looks at names, so it also reports functions that don’t come from Node.js.

fs.existsSync(somePath);
code-block.js:1:1 lint/nursery/noSyncMethods ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Unexpected call to the synchronous function existsSync.

> 1 │ fs.existsSync(somePath);
│ ^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ Synchronous functions block the program until they finish, so nothing else can run in the meantime.

ℹ Use the asynchronous version of this function instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

function foo() {
var contents = fs.readFileSync(somePath).toString();
}
code-block.js:2:20 lint/nursery/noSyncMethods ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Unexpected call to the synchronous function readFileSync.

1 │ function foo() {
> 2 │ var contents = fs.readFileSync(somePath).toString();
│ ^^^^^^^^^^^^^^^^^^^^^^^^^
3 │ }
4 │

ℹ Synchronous functions block the program until they finish, so nothing else can run in the meantime.

ℹ Use the asynchronous version of this function instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

obj.sync();
const contents = await fs.promises.readFile(somePath);
const readFile = fs.readFileSync;

Default: false

When true, the rule allows synchronous calls at the top level of a file, outside of any function. Code at the top level runs once, when the file is first loaded, so blocking there doesn’t slow down later work. Calls inside functions and methods are still reported, and so are calls in the values of class properties that aren’t static, because those values are computed each time the class creates an object.

biome.json
{
"linter": {
"rules": {
"nursery": {
"noSyncMethods": {
"level": "on",
"options": {
"allowAtRootLevel": true
}
}
}
}
}
}
const read = (file) => fs.readFileSync(file);
code-block.js:1:24 lint/nursery/noSyncMethods ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Unexpected call to the synchronous function readFileSync.

> 1 │ const read = (file) => fs.readFileSync(file);
│ ^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ Synchronous functions block the program until they finish, so nothing else can run in the meantime.

ℹ Use the asynchronous version of this function instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

const config = fs.readFileSync("config.json").toString();

Default: []

A list of function names that the rule doesn’t report.

biome.json
{
"linter": {
"rules": {
"nursery": {
"noSyncMethods": {
"level": "on",
"options": {
"ignore": [
"readFileSync"
]
}
}
}
}
}
}
fs.readdirSync(somePath);
code-block.js:1:1 lint/nursery/noSyncMethods ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Unexpected call to the synchronous function readdirSync.

> 1 │ fs.readdirSync(somePath);
│ ^^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ Synchronous functions block the program until they finish, so nothing else can run in the meantime.

ℹ Use the asynchronous version of this function instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

fs.readFileSync(somePath);