Skip to content

useConsistentJsonFileRead (JavaScript)

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

Enforce a consistent way of reading JSON files before passing them to JSON.parse().

The encoding of a file tells Node.js how to turn the file’s bytes into text, for example 'utf8'. With an encoding, fs.readFile() and fs.readFileSync() return a string. Without one, they return a Buffer, which holds the file’s raw bytes. JSON.parse() accepts both: JavaScript turns a Buffer into a string automatically, using UTF-8. This rule makes every JSON file read use the same style.

By default, the rule asks for strings, because TypeScript reports an error when a Buffer is passed to JSON.parse(). Use the readAs option to ask for Buffers instead.

The rule checks readFile() and readFileSync() calls whose result is passed to JSON.parse(), either directly or through a variable that is used only once. It checks these methods on any object, such as fs.readFile() or fs.promises.readFile(), and functions imported by name from fs, node:fs, fs/promises, or node:fs/promises. Calls that pass options other than encoding, such as signal, aren’t checked.

const packageJson = JSON.parse(await fs.readFile("./package.json"));
code-block.js:1:38 lint/nursery/useConsistentJsonFileRead  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This JSON file is read as a Buffer before being passed to JSON.parse().

> 1 │ const packageJson = JSON.parse(await fs.readFile("./package.json"));
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ JSON.parse() expects a string. A Buffer only works because JavaScript turns it into a string automatically, and TypeScript reports it as an error.

ℹ Read the file with the 'utf8' encoding 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.

ℹ Unsafe fix: Read the file with the 'utf8' encoding.

1 │ const·packageJson·=·JSON.parse(await·fs.readFile("./package.json",·"utf8"));
│ ++++++++
const promise = fs.readFile("./package.json");
const packageJson = JSON.parse(await promise);
code-block.js:1:17 lint/nursery/useConsistentJsonFileRead  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This JSON file is read as a Buffer before being passed to JSON.parse().

> 1 │ const promise = fs.readFile("./package.json");
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │ const packageJson = JSON.parse(await promise);
3 │

ℹ JSON.parse() expects a string. A Buffer only works because JavaScript turns it into a string automatically, and TypeScript reports it as an error.

ℹ Read the file with the 'utf8' encoding 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.

ℹ Unsafe fix: Read the file with the 'utf8' encoding.

1 │ const·promise·=·fs.readFile("./package.json",·"utf8");
│ ++++++++
import { readFileSync } from "node:fs";
const packageJson = JSON.parse(readFileSync("./package.json", { encoding: null }));
code-block.js:2:63 lint/nursery/useConsistentJsonFileRead  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This JSON file is read as a Buffer before being passed to JSON.parse().

1 │ import { readFileSync } from "node:fs";
> 2 │ const packageJson = JSON.parse(readFileSync("./package.json", { encoding: null }));
│ ^^^^^^^^^^^^^^^^^^
3 │

ℹ JSON.parse() expects a string. A Buffer only works because JavaScript turns it into a string automatically, and TypeScript reports it as an error.

ℹ Read the file with the 'utf8' encoding 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.

ℹ Unsafe fix: Read the file with the 'utf8' encoding.

1 1 │ import { readFileSync } from "node:fs";
2 │ - const·packageJson·=·JSON.parse(readFileSync("./package.json",·{·encoding:·null·}));
2 │ + const·packageJson·=·JSON.parse(readFileSync("./package.json",·"utf8"));
3 3 │

const packageJson = JSON.parse(await fs.readFile("./package.json", "utf8"));
// Not checked, because the options include more than `encoding`.
const promise = fs.readFile("./package.json", { encoding: null, signal });
const config = JSON.parse(await promise);

Default: "string"

How JSON files should be read before they are passed to JSON.parse():

  • "string": read files with the 'utf8' encoding. The rule reports reads without an encoding, or with a null or undefined encoding.
  • "buffer": read files without an encoding. The rule reports reads with the 'utf8' or 'utf-8' encoding.

The following examples use "buffer":

biome.json
{
"linter": {
"rules": {
"nursery": {
"useConsistentJsonFileRead": {
"level": "on",
"options": {
"readAs": "buffer"
}
}
}
}
}
}
const packageJson = JSON.parse(await fs.readFile("./package.json", "utf8"));
code-block.js:1:68 lint/nursery/useConsistentJsonFileRead  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This JSON file is read as a string, but this project reads JSON files as Buffers.

> 1 │ const packageJson = JSON.parse(await fs.readFile("./package.json", "utf8"));
│ ^^^^^^
2 │

ℹ JSON.parse() turns a Buffer into a string using UTF-8 automatically, so the encoding isn't needed.

ℹ Remove the encoding.

ℹ 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.

ℹ Unsafe fix: Remove the encoding.

1 │ const·packageJson·=·JSON.parse(await·fs.readFile("./package.json",·"utf8"));
│ --------
const packageJson = JSON.parse(fs.readFileSync("./package.json", { encoding: "utf8" }));
code-block.js:1:66 lint/nursery/useConsistentJsonFileRead  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This JSON file is read as a string, but this project reads JSON files as Buffers.

> 1 │ const packageJson = JSON.parse(fs.readFileSync("./package.json", { encoding: "utf8" }));
│ ^^^^^^^^^^^^^^^^^^^^
2 │

ℹ JSON.parse() turns a Buffer into a string using UTF-8 automatically, so the encoding isn't needed.

ℹ Remove the encoding.

ℹ 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.

ℹ Unsafe fix: Remove the encoding.

1 │ const·packageJson·=·JSON.parse(fs.readFileSync("./package.json",·{·encoding:·"utf8"·}));
│ ----------------------
const packageJson = JSON.parse(await fs.readFile("./package.json"));
const config = JSON.parse(fs.readFileSync("./config.json", null));