eslint: Error: Unexpected end of JSON input when using empty package.json

The version of ESLint you are using. 6.6.0

The problem you want to solve. When I have an empty package.json file in nested folder, near checked files, eslint gives error:

Error: Unexpected end of JSON input

There were similar issues previously - https://github.com/eslint/eslint/issues/11026#issuecomment-433744443, https://github.com/eslint/eslint/issues/7748 and there was no error in 5.16.0. Since 6.0.0 an error is thrown.

Your take on the correct solution to problem. It would be nice to have an option to disable this error. I tried to ignore this json file, but didn’t succeed.

This is useful for me because I want this file to be invalid on purpose so later I can check it.

Are you willing to submit a pull request to implement this change? Probably not, no free time to dive in atm.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 18 (12 by maintainers)

Most upvoted comments

I think we should treat invalid or empty object package.json the same way as we treat lack of configuration files of any other type. The only thing in package.json that counts as an ESLint configuration is an eslintConfig key and object. Anything else should not matter to ESLint.

I think we should throw if the eslintConfig value is invalid for some reason (e.g., it’s an array or string rather than an object). But, if we can’t even see an eslintConfig key, then there is no configuration for ESLint to look at and we know this for a fact. Throwing on invalid JSON (for package.json) is unnecessarily intrusive, in my opinion.

When I try to lint tests folder, I believe eslint tries to read this nested tests/bad-json/package.json file to find eslint configuration and gives me an error.

You are right. In that case, you need /tests/bad-json in your .eslintignore, or a valid ESLint configuration file that has a higher priority than package.json such as /tests/bad-json/.eslintrc.yml.

Because ESLint reads /tests/bad-json/package.json when it entered /tests/bad-json directory in order to check if the config file change how ESLint traverses the subdirectory via RFC20 or RFC22. (so /tests/bad-json/package.json can have a setting that ignores/unignores /tests/bad-json/*.)

Appreciate all the info! I think the suggestion here is the right solution to this. As someone who works on language tooling, I’ve encountered these kinds of issues a lot, and what I tend to see done is to put test fixture files in a test/fixtures directory and ignore the whole directory when linting.

Honestly, I think reasonable if we going to deprecate config files in subdirectories.