Skip to content

How to resolve EJSONPARSE error when using 'npm install xxx'?

1. Purpose

In this post, I will demonstrate how to resolve the following exception or error when initializing a project using the npm install command.

Terminal window
learn-vue npm install vue
npm ERR! code EJSONPARSE
npm ERR! file /Users/bswen/js-projects/learn-vue/package.json
npm ERR! JSON.parse Failed to parse json
npm ERR! JSON.parse Unexpected end of JSON input while parsing near ''
npm ERR! JSON.parse Failed to parse package.json data.
npm ERR! JSON.parse package.json must be actual JSON, not just JavaScript.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/bswen/.npm/_logs/2021-03-23T10_27_08_820Z-debug.log
learn-vue

We are installing the ‘vue’ module to our JavaScript project, and the working directory is as follows:

Terminal window
/Users/bswen/js-projects/learn-vue
.
└── /Users/bswen/js-projects/learn-vue/
├── readme.txt
└── calc.js

2. The reason and solution

Before resolving this problem, we should understand the concept of npm:

npm is the package manager for the Node JavaScript platform. It puts modules in place so that Node can find them and manages dependency conflicts intelligently. It is extremely configurable to support a wide variety of use cases. Most commonly, it is used to publish, discover, install, and develop Node programs.

2.1 Reason

The npm module requires a package.json to store metadata of dependencies, but there is no package.json in our project yet.

2.2 How to create package.json

We must create the package.json before we use npm install xxx to add dependencies to our project.

Just use npm init as follows:

npm
learn-vue npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (learn-vue) learn-vue
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /Users/bswen/js-projects/learn-vue/package.json:
{
"name": "learn-vue",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this OK? (yes) yes

About npm init:

Terminal window
npm init <initializer> can be used to set up a new or existing npm package.
initializer in this case is an npm package named create-<initializer>, which will be installed by npx, and then have its main bin executed -- presumably creating or updating package.json and running any other initialization-related operations.
The init command is transformed to a corresponding npx operation as follows:
npm init foo -> npx create-foo
npm init @usr/foo -> npx @usr/create-foo
npm init @usr -> npx @usr/create
Any additional options will be passed directly to the command, so npm init foo -- --hello will map to npx create-foo --hello.

Now the project structure is:

Terminal window
/Users/bswen/js-projects/learn-vue
readme.txt
calc.js
package.json

What’s inside the package.json? Here it is:

package.json
{
"name": "learn-vue",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
}
}

2.3 Now try again

Now we can try again to install the module we need as follows:

npm
learn-vue npm install vue
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN [email protected] No description
npm WARN [email protected] No repository field.
added 1 package from 1 contributor in 0.946s
learn-vue

Now let’s look at the package.json:

package.json
{
"name": "learn-vue",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"vue": "^2.6.12"
}
}

3. Summary

In this post, we demonstrated how to resolve the EJSONPARSE error when using the npm install xxx command to add JavaScript dependencies to our project. The key takeaway is to ensure that you always have a valid package.json file in your project before running npm install. This file is essential for managing dependencies and project metadata. By following the steps outlined above, you can avoid common pitfalls and streamline your development process.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!