Skip to content
← Back to rules

node/no-sync Style

What it does

Disallows synchronous methods from being called in Node.js code.

Why is this bad?

In Node.js, most I/O is done through asynchronous methods. However, there are often synchronous versions of the asynchronous methods. For example, fs.exists() and fs.existsSync(). In some contexts, using synchronous operations is okay (if, as with ESLint, you are writing a command line utility). However, in other contexts the use of synchronous operations is considered a bad practice that should be avoided.

Examples

Examples of incorrect code for this rule:

js
fs.existsSync(somePath);

function foo() {
  var contents = fs.readFileSync(somePath).toString();
}

Examples of correct code for this rule:

js
obj.sync();

async(function () {
  // ...
});

Configuration

This rule accepts a configuration object with the following properties:

allowAtRootLevel

type: boolean

default: false

Whether synchronous methods should be allowed at the top level of a file.

ignores

type: string[]

default: []

Function names to ignore.

How to use

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["node"],
  "rules": {
    "node/no-sync": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["node"],
  rules: {
    "node/no-sync": "error",
  },
});
bash
oxlint --deny node/no-sync --node-plugin

Version

This rule was added in vnext.

References