Npm install package json error

Fix for error package.json not found in npm install running in nodejs application Solutions for package.json not found, ENOENT no such file or …
package.json not found Nodejs

In this blog post, learn how to fix the “package.json not found” error in the npm command.
You can also check other posts on npm command deprecate option is deprecated and Fix for digital envelope routines::unsupported

Fix package.json not found an error

package.json is a JSON configuration file of a nodejs project which contains metadata of an application + dependencies etc.

In NPM based applications such as nodejs, Angular, VueJS, and ReactJS applications, the package.json file location is the application root.

When you are creating a new project manually or when installing project dependencies we used to get the following errors.

  • package.json not found
  • ENOENT: no such file or directory package.json
  • npm can’t find a package.json file
  • no such file or directory package.json

Usually, this error comes when npm commands are running and not found package.json in the application root.

This error gives after running the npm command.

  • npm installs dependencies locally or globally
  • npm run start — running the project

There are no ways we can create a package.json file.

  • manually creation using a text editor
  • npm init command
  • npm init -y command

npm init command for generating package.json

npm, init command creates package.json that is filled with values that are entered by the user. It asks promptly to enter details as below.

This is the starting phase for any npm-based application creation.

 B:Workspaceblognpmcommand>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 ` afterward to install a package and  
save it as a dependency in the package.json file.  
  
Press ^C at any time to quit.  
package name: (npmcommand) mypackage  
version: (1.0.0)  
description:  
entry point: (index.js)  
test command:  
git repository:  
keywords:  
author:  
license: (ISC)  
About to write to B:Workspaceblognpmcommandpackage.json:  
  
{  
  "name": "mypackage",  
  "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

It creates the below package.json.

 {  
  "name": "mypackage",  
  "version": "1.0.0",  
  "description": "",  
  "main": "index.js",  
  "scripts": {  
    "test": "echo "Error: no test specified" && exit 1"  
  },  
  "author": "",  
  "license": "ISC"  
}

npm init -y with default package.json

This will not ask for any details and just creates a package.json file with default values. You can later change or modify using any text editor

The above command creates a default package json file Here is a package.json example.

{  
  "name": "npmcommand",  
  "version": "1.0.0",  
  "description": "",  
  "main": "index.js",  
  "scripts": {  
    "test": "echo "Error: no test specified" && exit 1"  
  },  
  "keywords": [],  
  "author": "",  
  "license": "ISC"  
}  

Once package.json is installed, You can install all the dependencies locally npm install –save-dev or npm install -g

Brad Dettmer

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, 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:

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!


Photo from Unsplash

The npm install command is used for installing JavaScript packages on your local computer.

When developing web projects, you may see issues that cause the npm install command to fail.

You need to see the error message generated in the terminal for clues to resolve the error.

Some of the most common issues for npm install not working are as follows:

  • npm command not found
  • No package.json file describing the dependencies
  • Integrity check failed error
  • For Windows: Python not found

Let’s see how you can resolve these errors next.

The npm command not found error

To run the npm install command, you need to have npm installed on your computer

$ npm install
sh: command not found: npm

The error above happens when npm can’t be found under the PATH environment variable.

First, you need to make sure that npm is installed on your computer.

npm is bundled with Node.js server, which you can download from the nodejs.org website.

Once you downloaded and installed Node.js, open the terminal and run the npm -v command.

You should see the version of npm installed on your computer as follows:

Once you see the npm version, you should be able to run the npm install command again.

For more details, visit the fixing npm command not found error article I’ve written previously.

ENOENT: No package.json file

The npm install command looks for a package.json file in order to find the packages it needs to install.

You need to make sure you have a package.json file right in the current directory where you run the command.

You can run the ls -1 command as follows:

$ ls -1
index.js
package-lock.json
package.json

Once you see there’s a package.json file in the output as shown above, then you can run the npm install command.

For this reason, you should always run the npm install command from the root directory of your project.

By the way, npm install will install packages already listed under the dependencies object of your package.json file.

Suppose you have a package.json file as shown below:

{
  "name": "n-app",
  "version": "1.0.0",
  "description": "A Node application",
  "dependencies": {
    "jest": "^28.1.1",
    "typescript": "^4.5.5",
    "webpack": "^2.7.0"
  }
}

Then npm install will install jest, typescript, and webpack to the generated node_modules/ folder.

If you need to install a new package, then you need to specify the name like these examples:

# 👇 install react package
npm install react

# 👇 install react, react-dom, and axios packages
npm install react react-dom axios

You can install one or many packages with the npm install command.

Integrity check failed error

When you have a package-lock.json file in your project, then npm will check the integrity of the package you downloaded with the one specified in the lock file.

When the checksum of the package is different, then npm will stop the installation and throw the integrity check failed error.

The following is an example of the error:

npm ERR! code EINTEGRITY
npm ERR! Verification failed while extracting @my-package@^1.2.0:
npm ERR! Verification failed while extracting @my-package@^1.2.0:
npm ERR! Integrity check failed:

npm ERR! Wanted: sha512-lQ...HA== 
npm ERR! Found:  sha512-Mj...vg==

When this happens, you can fix the issue by verifying the cache:

When the command is finished, run npm install again.

If you still see the error, then delete your lock file and clean the npm cache.

Run the following commands from your project’s root directory:

# 👇 remove the lock file
rm package-lock.json

# 👇 remove the node_modules folder
rm -rf node_modules

# 👇 Clear the npm cache
npm cache clean --force

# 👇 run npm install again
npm install

That should fix the issue with the integrity check.

For Windows only: Python not found

When running npm install on Windows, you may see an error that says Can’t find Python executable as shown below:

> node-gyp rebuild

/n-app>node "..node_modulesnode-gypbinnode-gyp.js" rebuild
npm http 304 https://registry.npmjs.org/bcrypt
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "python",
you can set the PYTHON env variable.

The node-gyp module is a Node.js tool for compiling native modules written in C and C++. It requires Python to run properly.

Instead of installing Python by yourself, you can install the Windows Build Tools which has Python included.

This is because Windows also requires Visual Studio build tools to make node-gyp run successfully.

If you install only Python, then you may find that you need the build tools later.

Please note that the latest version of Node.js for Windows installer already includes the build tools by default.

But if you can’t update your Node.js version yet, then installing the build tools manually should help you fix this error.

Conclusion

The npm install command may fail to work because of many reasons.

When the command doesn’t work, you need to check the output first and see what specific error you have.

Try a Google search to fix the issue. When you don’t see a solution, try posting a new question at Stack Overflow mentioning the things you have tried to fix the issue.

Good luck resolving the issue! 👍

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.

Понравилась статья? Поделить с друзьями:

Читайте также:

  • Npm install node sass error
  • Npm install g npm ошибка
  • Npm install error self signed certificate
  • Npm install error node gyp
  • Npm init y ошибка

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии