Tuesday, October 23, 2018

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

In this blog post, We are going to learn how to fix package.json not found in npm command.

npm install package.json error

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

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

When you are creating a new project manually or when installing dependencies of a project 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 package.json is not found in the application root
This error gives during Below npm command running
  •  npm install dependencies locally or globally
  •  npm run start - running the project
 There are no ways we can create a package.json file
  • manually creation using 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
This creates 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 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 default package json file Here is 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

Related article


EmoticonEmoticon