You clone a repo, or maybe you are creating your project with a new
package.json file. When you try to
npm install package_name — save-dev, ornpm i npm`, the JSON doesn’t parse.
npm ERR! code EJSONPARSE
npm ERR! JSON.parse Failed to parse json
npm ERR! JSON.parse Unexpected token…
This error could occur because of any number of reasons. You think something is probably wrong with your
package.json file, but you’re not exactly sure what it is. Maybe you try
yarn install because Yarn is a package manager similar to npm that’s known to be faster and have more enhancements. But that probably doesn’t help if the issue is with your JSON. Here are some possible solutions or ways to diagnose the issue:
npm cache clean -force: Cleaning your cache will resolve potential conflicts with previously installed packages.
2.) Debug log: This might point you to the exact location where the error is occurring so you can fix it. You can find the location of the debug log in the console output. For example,
3.) JSON validator: Use a tool like JSONLint to check and make sure your package.json file is valid. A JSON validator or a linter should give you more insight as to why your file is not parsing.
4.) Check commas: In some cases, adding a comma between key value pairs may help. In my case, deleting a trailing comma resolved the error.
5.) Minify: A minifier could remove extra white space or improper formatting and resolve the issue.
6.) Number of packages: Make sure you just have one package in your package.json. It’s better to split your project into multiple directories if you want to use multiple packages. Every component requires its own package.jsonto avoid versioning issues or to make require-statement resolution works.
rm package-lock.json: Removing the lock file and running
npm install again could help resolve versioning issues.
Hopefully one of these solutions helped you. Please add your ideas or any other solutions you may have in the comments!
Thank you Angie Jones for teaching me the listicle blogging style, Jess Lee for reviewing this blog post and providing awesome feedback, Write Speak Code for inspiring me to write it, and Recurse Center for being a wonderful programming community!