The npm ERR! Unexpected end of JSON input while parsing near occurs while working on node.js when there is some issue with the npm (Node Package Manager) registry or the npm cache is corrupted. In this article, we will discuss different methods to resolve this error.
Solutions to Fix the npm ERR! Unexpected end of JSON input while parsing near:
Following are some possible methods that can be used to resolve this issue. Try and see which one worked for you.
Clear npm Cache
Run the following command to clear the npm cache:
npm cache clean --force
After clearing the cache, run the npm command again and see if it will resolve the error. If not, install the latest version of npm. Run the following command to update npm:
npm i npm@latest -g
Now, Retry the npm command that resulted in the error. For example, if you were installing a package, run:
npm install
Note: Running npm commands with the –verbose option can provide more information about the error. For example:
npm install --verbose
Check the output for more details about the error.
Registry Configuration
Check if the npm registry is configured correctly. If not, try to switch to the npm registry from the below list and see which one works for you.
- https://registry.npmjs.org/ (Default)
- https://skimdb.npmjs.com/registry
- https://registry.npm.taobao.org/
- http://r.cnpmjs.org/
- https://npm.open-registry.dev/ (https://open-registry.dev)
- http://registry.mirror.cqupt.edu.cn
- https://registry.nodejitsu.com/
Run the following commands to set the registry to the default npm registry:
npm config set registry https://registry.npmjs.org/
After running the above command clear the npm cache and verify it using the below code
npm cache clear --force
npm cache verify
Now, the npm ERR! Unexpected end of JSON input while parsing near error will be resolved.
Remove the package-lock.json
If the issue persists, you can try to remove the package-lock.json file.
For Windows:
Run the below command in PowerShell administrator mode.
Remove-Item package-lock.json -Force
For Linux:
Run the below command if you are working on the Linux operating system.
rm -rf package-lock.json
Path In Azure Devops:
$(System.DefaultWorkingDirectory)\myAppName\ClientApp\node_modules'
Note: Replace the myAppName with your source code path.
Hopefully by removing package-lock.json error will be resolved.
Reinstall Node.js
If none of the above solutions work, consider reinstalling Node.js and npm. This ensures that you have a clean installation. After reinstalling Node.js, you may need to re-run npm install for your project
Conclusion:
In this article, we have learned how to tackle the npm ERR! Unexpected end of JSON input while parsing near error. Throughout this troubleshooting journey, we’ve explored various strategies, from clearing the npm cache and updating npm to verifying registry configurations and removing the package-lock.json. Try these methods one by one and see which one works for you. If you have found a method that is not discussed please share it in the comment section.
Happy Learning 🙂
- Fix – ‘User application exited with status 1’ in Spark - March 21, 2024
- Unexpected EOF Encountered in BCP Data-File: How to Resolve Import/Export Errors - March 13, 2024
- RESOLVED: ‘Configure: error: unexpected output of ‘arch’ on os’ - March 13, 2024