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'));