So I was just about to setup this login form for my page using node.js
I ran
npm init
and went trough the various steps to complete it. When I was all done with that, and the package.json file had been created I added these lines of code»
dependencies": {
"bcryptjs": "*",
"body-parser": "*",
"connect-flash": "*",
"cookie-parser": "^1.4.1",
"express": "*",
"express-handlebars": "*",
"express-messages": "*",
"express-session": "*",
"express-validator": "*",
"mongodb": "*",
"mongoose": "*",
"passport": "*",
"passport-http": "*",
"passport-local": "*" }
I now ran the command
npm install
But ended up getting these ERRORS
npm ERR! file /Users/albingroen/Desktop/newProject/package.json
npm ERR! code EJSONPARSE
npm ERR! Failed to parse json
npm ERR! Unexpected token 'n' at 26:5
npm ERR! }
npm ERR! ^
npm ERR! File: /Users/albingroen/Desktop/newProject/package.json
npm ERR! Failed to parse package.json data.
npm ERR! package.json must be actual JSON, not just JavaScript.
npm ERR!
npm ERR! This is not a bug in npm.
npm ERR! Tell the package author to fix their package.json file. JSON.parse
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/albingroen/.npm/_logs/2017-04-04T18_52_34_920Z-debug.log
I have tried following things:
-
Cleaning the node cache
-
Using different terminal
-
Updating node.js
-
Updating mongodb
-
Changing location of package.json
-
Running npm install without the added dependencies
-
Deleting the nodemodules mapp
I really don’t know what to do anymore. Somebody got an idea?
You clone a repo, or maybe you are creating your project with a new package.json
file. When you try to npm install
, npm install package_name — save-dev, or
npm 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:
1.) 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.
7.) 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!
1. Purpose
In this post, I would demo how to solve the below exception or error when we initialize a project using npm install
command.
➜ 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, the working directory is as follows:
/Users/bswen/js-projects/learn-vue
.
└── /Users/bswen/js-projects/learn-vue/
├── readme.txt
└── calc.js
why?
2. The reason and solution
Before solve this problem, we should know 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 needs a package.json to store metadata of dependencies, but there is no package.json in our project yet.
If you plan to publish your package, the most important things in your package.json are the name and version fields as they will be required. The name and version together form an identifier that is assumed to be completely unique. Changes to the package should come along with changes to the version. If you don’t plan to publish your package, the name and version fields are optional..
2.2 How to create package.json
We must create the package.json before we use ‘npm install xxx’ to add dependency to our project.
Just use ‘npm init’ as follows:
➜ 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:
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:
/Users/bswen/js-projects/learn-vue
readme.txt
calc.js
package.json
What’s inside the package.json? Here it is:
➜ learn-vue cat 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:
➜ 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.
+ [email protected]
added 1 package from 1 contributor in 0.946s
➜ learn-vue
Now let’s look at the package.json:
➜ learn-vue cat 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 solve the EJSONPARSE error when using ‘npm install xxxx’ command to add javascript dependencies to our project. Actually you should make sure that you always have a ready ‘package.json’ in your project. There is a trick in it. Thanks for your reading. Regards.
Hello, Can someone please help me with this error? I cannot seem to find what is wrong with my package.JSON file. Suddenly I am unable to npm install modules. This is something I am working on for school. I am getting npm ERR! file package.json npm ERR! code EJSONPARSE npm ERR! Failed to parse json npme ERR! Unexpected tocken, in JSON at position 467 while parsing near ‘…th»:»0.12.7″
I believe this is regarding line 21 «path»: «0.12.7″ However, I have had no success at fixing this error. Below is my package.JSON
{ «name»: «shopping_cart», «version»: «1.0.0», «description»: «», «main»: «cart_server.js», «directories»: { «lib»: «lib» }, «scripts»: { «test»: «echo «Error: no test specified» && exit 1″ }, «author»: «», «license»: «ISC», «dependencies»: { «basic-auth-connect»: «1.0.0″, «body-parser»: «1.18.2″, «ejs»: «2.5.9″, «express»: «4.16.3″, «mongodb»: «3.0.7″, «mongoose»: «5.0.16″, «path»: «0.12.7″ } } { «name»: «authtest», «description»: «Testing passport», «version»: «0.0.1», «private»: true, «dependencies»: { «body-parser»: «1.x», «cookie-parser»: «1.x», «express»: «4.x», «express-flash»: «0.x», «express-session»: «1.x», «passport» : «0.3.x», «passport-local»: «1.x» } }
Thank you!
So I was just about to setup this login form for my page using node.js
I ran
npm init
and went trough the various steps to complete it. When I was all done with that, and the package.json file had been created I added these lines of code»
dependencies": {
"bcryptjs": "*",
"body-parser": "*",
"connect-flash": "*",
"cookie-parser": "^1.4.1",
"express": "*",
"express-handlebars": "*",
"express-messages": "*",
"express-session": "*",
"express-validator": "*",
"mongodb": "*",
"mongoose": "*",
"passport": "*",
"passport-http": "*",
"passport-local": "*" }
I now ran the command
npm install
But ended up getting these ERRORS
npm ERR! file /Users/albingroen/Desktop/newProject/package.json
npm ERR! code EJSONPARSE
npm ERR! Failed to parse json
npm ERR! Unexpected token 'n' at 26:5
npm ERR! }
npm ERR! ^
npm ERR! File: /Users/albingroen/Desktop/newProject/package.json
npm ERR! Failed to parse package.json data.
npm ERR! package.json must be actual JSON, not just JavaScript.
npm ERR!
npm ERR! This is not a bug in npm.
npm ERR! Tell the package author to fix their package.json file. JSON.parse
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/albingroen/.npm/_logs/2017-04-04T18_52_34_920Z-debug.log
I have tried following things:
-
Cleaning the node cache
-
Using different terminal
-
Updating node.js
-
Updating mongodb
-
Changing location of package.json
-
Running npm install without the added dependencies
-
Deleting the nodemodules mapp
I really don’t know what to do anymore. Somebody got an idea?
Questions : Npm ERR code EJSONPARSE — Failed to parse json
2023-02-06T12:47:13+00:00 2023-02-06T12:47:13+00:00
635
So I was just about to setup this login form post uvdos node.js for my page using node.js
I ran
npm init
and went trough the various steps to post uvdos node.js complete it. When I was all done with that, post uvdos node.js and the package.json file had been created I post uvdos node.js added these lines of code»
dependencies": {
"bcryptjs": "*",
"body-parser": "*",
"connect-flash": "*",
"cookie-parser": "^1.4.1",
"express": "*",
"express-handlebars": "*",
"express-messages": "*",
"express-session": "*",
"express-validator": "*",
"mongodb": "*",
"mongoose": "*",
"passport": "*",
"passport-http": "*",
"passport-local": "*" }
I now ran the command
npm install
But ended up getting these ERRORS
npm ERR! file /Users/albingroen/Desktop/newProject/package.json
npm ERR! code EJSONPARSE
npm ERR! Failed to parse json
npm ERR! Unexpected token 'n' at 26:5
npm ERR! }
npm ERR! ^
npm ERR! File: /Users/albingroen/Desktop/newProject/package.json
npm ERR! Failed to parse package.json data.
npm ERR! package.json must be actual JSON, not just JavaScript.
npm ERR!
npm ERR! This is not a bug in npm.
npm ERR! Tell the package author to fix their package.json file. JSON.parse
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/albingroen/.npm/_logs/2017-04-04T18_52_34_920Z-debug.log
I have tried following things:
-
Cleaning the node cache
-
Using different terminal
-
Updating node.js
-
Updating mongodb
-
Changing location of package.json
-
Running npm install without the added post uvdos node.js dependencies
-
Deleting the nodemodules mapp
I really don’t know what to do anymore. post uvdos node.js Somebody got an idea?
Total Answers 7
28
Answers 1 : of Npm ERR code EJSONPARSE — Failed to parse json
I had the same error. It is caused by solved uvdos json wrong syntax within your package.json.
I solved uvdos json solved it by removing an unnecessary ‘,’ solved uvdos json at the end of my last dependency, but it solved uvdos json can be different for everyone. Looking solved uvdos json over the package.json syntax would be solved uvdos json the best bet.
0
2023-02-06T12:47:13+00:00 2023-02-06T12:47:13+00:00Answer Link
mRahman
6
Answers 2 : of Npm ERR code EJSONPARSE — Failed to parse json
Just delete the unnecessary commas, that solved uvdos json is, at the end of each object in your solved uvdos json package.json.
My path: solved uvdos json SolutionName/ClientApp/src/package.json
In solved uvdos json my case it was in «lodash».
"resolutions": {
"url-parse": "> = 1.5.0",
"lodash": "> = 4.17.21", <-- I deleted this comma!
}
0
2023-02-06T12:47:13+00:00 2023-02-06T12:47:13+00:00Answer Link
yousuf
4
Answers 3 : of Npm ERR code EJSONPARSE — Failed to parse json
Like the above answer, i checked my solved uvdos json files for any unnecessary ‘,’ but solved uvdos json everything looked good.
I tried this in terminal, and it worked.
npm run start
and then, install your dependencies, for solved uvdos json me i did it as
npm install express mongoose
0
2023-02-06T12:47:13+00:00 2023-02-06T12:47:13+00:00Answer Link
yousuf
1
Answers 4 : of Npm ERR code EJSONPARSE — Failed to parse json
You must be missing some «}» or solved uvdos json something in your package.json file just solved uvdos json check it out or compare it with the old solved uvdos json version of your file it helped me out solved uvdos json many times
0
2023-02-06T12:47:13+00:00 2023-02-06T12:47:13+00:00Answer Link
rohim
5
Answers 5 : of Npm ERR code EJSONPARSE — Failed to parse json
I have the same error because I forget solved uvdos json to give a comma «,» in my JSON file. solved uvdos json After putting comma in JSON file it solved uvdos json worked fine.
0
2023-02-06T12:47:13+00:00 2023-02-06T12:47:13+00:00Answer Link
yousuf
6
Answers 6 : of Npm ERR code EJSONPARSE — Failed to parse json
Check Whether You have created the solved uvdos json package.json file. If not create it and solved uvdos json initialize it with {}.
0
2023-02-06T12:47:13+00:00 2023-02-06T12:47:13+00:00Answer Link
joya
6
Answers 7 : of Npm ERR code EJSONPARSE — Failed to parse json
In my react project ,in package.json solved uvdos json name of my project was given in a solved uvdos json letter.When i changed it to a word in solved uvdos json small case command worked for e then.
0
2023-02-06T12:47:13+00:00 2023-02-06T12:47:13+00:00Answer Link
yousuf