noSyncMethods (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.5 - Diagnostic Category:
lint/nursery/noSyncMethods - This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Same as
n/no-sync
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noSyncMethods": "error" } } }}Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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;Options
Section titled “Options”allowAtRootLevel
Section titled “allowAtRootLevel”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.
{ "linter": { "rules": { "nursery": { "noSyncMethods": { "level": "on", "options": { "allowAtRootLevel": true } } } } }}Invalid
Section titled “Invalid”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();ignore
Section titled “ignore”Default: []
A list of function names that the rule doesn’t report.
{ "linter": { "rules": { "nursery": { "noSyncMethods": { "level": "on", "options": { "ignore": [ "readFileSync" ] } } } } }}Invalid
Section titled “Invalid”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);Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.