# consistent-json-file-read πŸ“ Enforce consistent JSON file reads before `JSON.parse()`. πŸ’ΌπŸš« This rule is enabled in the βœ… `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). This rule is _disabled_ in the β˜‘οΈ `unopinionated` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). 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()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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. ## Examples ```js // ❌ 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')); ``` ```js // βœ… const promise = fs.readFile('./package.json', {encoding: 'utf8', signal}); const packageJson = JSON.parse(await promise); ``` ## Options Type: `'string' | 'buffer'` Default: `'string'` ### `buffer` Prefer reading JSON files as buffers. ```js // 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')); ```