Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
npm
json

What is package.json in npm?

Kedar Kodgire

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The package.json is the principal place to configure and define how to interact with and manage your application. For example, the npm CLI uses this file to start your project, install dependencies, run scripts, etc.

Here is an example of the package.json file:

{
  "name": "twitter-automation",
  "version": "1.0.0",
  "description": "twitter automatoin for blog posting",
  "main": "server.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node backend/server",
    "server": "nodemon backend/server",
    "client": "npm start --prefix frontend",
    "dev": "concurrently \"npm run server\" \"npm run client\""
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/Kedar-K/twitter-automation.git"
  },
  "author": "Kedar Kodgire",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/Kedar-K/twitter-automation/issues"
  },
  "homepage": "https://github.com/Kedar-K/twitter-automation#readme",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.19.0",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "express-async-handler": "^1.1.4",
    "mongoose": "^5.11.12",
    "twit": "^2.2.11",
  },
  "devDependencies": {
    "concurrently": "^5.3.0",
    "nodemon": "^2.0.7"
  }
}

Let’s look at the file contents one by one.

  • name: The name specified in package.json is the identifier of the package. If you plan to publish it on npm, this field is required. Otherwise, it’s optional.

  • version: This will describe the variant of the package. Again, if you plan to publish, this version is a required field. name and version together form a unique identifier. Any changes to the package should come along with the changes in version number.

  • description: This will be a string and should provide more information about your project.

  • main: This field is a primary entry point of your program. If it is not specified, it defaults to index.js in the package’s root folder.

  • type: The type field defines the module format Node.js uses for all .js files that have the package.json file as their nearest parent.

  • scripts: The commands that run at various moments in the lifecycle of your package are specified here. This field is a dictionary. For example, we can use the npm start command in our case, which will trigger the node backend/server command specified in the package.json file.

  • repository: Here, you specify the location of your code.

  • author: Here, you specify the contributor details. For example, "Kedar Kodgire <k@kodgire.com> (http://kedar.tumb.com/)"

  • license: You should specify a license for your package so that people know how they are authorized to use it and any restrictions you’re laying on it. You can check the complete list of SPDX license IDs. Ideally, you should choose one that is OSI-approved.

  • bugs: This should point to the location of your bugs, for example, GitHub repo issues.

  • homepage: The URL to your project’s home page.

  • dependencies: Dependencies is a simple object that maps a package name to a version range. This version range is a string with one or more space-separated descriptors.

  • devDependencies: This is similar to the one above. Still, if someone plans to download and use your module in another program, there is no need to download these dependencies.

RELATED TAGS

communitycreator
npm
json

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring