prefer-json-parse-buffer

March 27, 2026 ยท View on GitHub

๐Ÿ“ Prefer reading a JSON file as a buffer.

๐Ÿšซ This rule is disabled in the following configs: โœ… recommended, โ˜‘๏ธ unopinionated.

๐Ÿ”ง This rule is automatically fixable by the --fix CLI option.

When reading and parsing a JSON file, it's unnecessary to read it as a string, because JSON.parse() can also parse Buffer.

Passing in a buffer may not be performant and is not compatible with TypeScript.

Examples

// โŒ
const packageJson = JSON.parse(await fs.readFile('./package.json', 'utf8'));

// โŒ
const promise = fs.readFile('./package.json', {encoding: 'utf8'});
const packageJson = JSON.parse(await promise);

// โœ…
const packageJson = JSON.parse(await fs.readFile('./package.json'));
// โœ…
const promise = fs.readFile('./package.json', {encoding: 'utf8', signal});
const packageJson = JSON.parse(await promise);
// โœ…
const data = JSON.parse(await fs.readFile('./file.json', 'buffer'));
// โœ…
const data = JSON.parse(await fs.readFile('./file.json', 'gbk'));