π Enforce consistent JSON file reads before JSON.parse().
πΌπ« This rule is enabled in the β
recommended config. This rule is disabled in the βοΈ unopinionated config.
π§ This rule is automatically fixable by the --fix CLI option.
When reading and parsing a JSON file, consistently read it using the configured style. By default, the rule prefers reading it as a string.
The default keeps the code compatible with TypeScript, where JSON.parse() accepts a string.
The rule only checks and fixes direct no-option reads and option objects where encoding is the only property. Reads with additional options are left unchanged to avoid dropping options.
// β
const packageJson = JSON.parse(await fs.readFile('./package.json'));
// β
const promise = fs.readFile('./package.json');
const packageJson = JSON.parse(await promise);
// β
const packageJson = JSON.parse(await fs.readFile('./package.json', 'utf8'));// β
const promise = fs.readFile('./package.json', {encoding: 'utf8', signal});
const packageJson = JSON.parse(await promise);Type: 'string' | 'buffer'
Default: 'string'
Prefer reading JSON files as buffers.
// eslint unicorn/consistent-json-file-read: ["error", "buffer"]
// β
const packageJson = JSON.parse(await fs.readFile('./package.json', 'utf8'));
// β
const packageJson = JSON.parse(await fs.readFile('./package.json'));