Fix for error package.json not found in npm install running

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:\Workspace\blog\npmcommand>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:\Workspace\blog\npmcommand\package.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

npm init -yes

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