Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript

How to create a package.json using node

Fahad Farid

A package.json is a JSON file located in the root of a JS project that holds relevant metadata for the project and manages a project’s dependencies, scripts, version, etc.

There are two ways to create a package.json file using node and yarn.

Using Node

If you have the latest version of node installed, run the following command to generate:

npm init

You can also add default values to the file by running:

npm init -y

Manually

To manually generate the package.json file, go to the root directory for the project and create a package.json file.

You then need to add the following code to the file filled with your values:

{
  "name": "",
  "version": ""
}

Properties of JSON file

Name

Name represents the names of the project. The name must be:

  • one word
  • lower case
  • not start with an ‘_’ or ‘.’

Version

The version denotes the current version of the modules. It must follow the semantic versioning guidelines, e.g., 1.0.0

Description

The description provides more information about the project.

Engines

The engine property is an object used to specify the library versions required for the application to run.

Dependencies

Dependencies denote the list of modules and packages required for the application to function. It is added to the dependency list when installed.

devDependencies

devDependencies are modules that are not required to run the application but are needed in the development phase.

You can run these with npm i package --save-dev.

Scripts

Scripts are key-value pairs used to perform a series of tasks such as building, testing, etc.

You can run these with npm run scriptname.

Main

Main is the entry point of the application and should refer to the main page of your app.

Homepage

Homepage is used to specify the landing page of the app.

Private

If the private property is set to true, the application cannot be published.

License

Valid licenses are added here.

Author

Author names are added here.

Repository

Repository is a key-value pair that contains the version control system used to manage the app.

You can specify the:

  • type of version control
  • URL of version control
  • optional directory within the repository

Bugs

*Link to the page where issues can be reported.

Keywords

An array of keywords that help to identify your project.

Custom Properties

Custom properties can be used to specify commands that can be found in the package documentation.

Here is an example that contains all of the properties stated above.

{
  "name": "package.json-educative",
  "version": "1.0.0",
  "description": "Master file of package.json",
  "private": true,
  "main": "index.js",
  "scripts": {
    "start": "node index",
    "dev": "nodemon index"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/Educative/package.json-educative.git"
  },
  "keywords": [
    "node",
    "package.json",
    "javascript",
    "npm"
  ],
  "author": "Fahad Farid",
  "license": "Standard",
  "homepage": "https://github.com/Educative/package.json-educative#readme",
  "engines": {
    "npm": "6.10.4",
    "node": "10.14.3"
  },
  "dependencies": {
  },
  "devDependencies": {
  }
}

RELATED TAGS

javascript

CONTRIBUTOR

Fahad Farid
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring