Package.json

Package.json is a JSON file that describes important metadata about a package, such as its name, version, dependencies, license, author, and more. Package.json must be included in version control and should not be in the .gitignore file.

What are Packages?

  • Packages are reusable units of code or libraries that can be published to the NPM registry, a centralized repository that stores and manages JavaScript packages and modules.

  • Packages provide various functionalities, such as utility functions, frameworks, tools, and more, that can be easily integrated into Node.js and JavaScript projects.

  • Packages in Node.js are managed and distributed via the NPM or Yarn package managers.

  • This command will publish the package located in the “my-package” folder to the npm registry using the default settings.

Syntax:

npm publish ./my-package

Why and where it is used?

  • Package.json is a key component of the Node.js ecosystem, as it helps developers manage project dependencies, automate common tasks, and provide important metadata about the project.

  • It also simplifies project setup and collaboration by documenting key project details and dependencies, making it easier for other developers to understand and contribute to the project.

  • It is located in the root directory of the project and is required before any packages can be installed from NPM.

Structure of package.json

The package.json file encompasses multiple attributes containing details about the project and its associated metadata.

Creating package.json

You can create package.json by using this command if you manually want to specify each attribute. Open your terminal and navigate to the root directory of your project, then run this command and follow the prompts:

npm init

or

You can also use the command given below. It is a shortcut for initializing a new package.json file with default values without requiring you to answer a series of interactive prompts:

npm init -y

Some attributes of package.json are as follows:

name: It establishes the package’s title, which should be unique within the npm registry and consist solely of lowercase characters, with a maximum length of 214 characters.

"name": "my-project"

version: It holds significant importance when publishing a package and is a mandatory requirement before proceeding with the publication process. This attribute represents the current version of the software described within the package.json file.

"version": "1.5.0"

license: It allows us to specify the applicable license for the code described. Typically, it uses identifier codes like “MIT” or “ISC” for corresponding licenses. Alternatively, “UNLICENSED” can be used.

"license": "ISC",

author: The author’s name or the responsible organization for the project, intended for individual person/entity.

"author": "John Williams john@example.com",

contributors: An array of contributors to the project, each with a name and email.

"contributors": [{
    "name": "Abc xyz",
    "email": "example@example.com",
    "url": "https://www.abc.com"
}],

description: A concise project or package description used for discoverability in the NPM registry. It serves as a brief summary of the package’s purpose.

"description": "This is a package for front end development",

main: This specifies the starting point for your project, which is typically the file used to initiate or load the project. If the “main” field is not specified in the package.json, Node.js will by default look for the index.js file as the starting point.

"main": "src/index.js",

scripts: It defines the scripts which are used for running tests or starting any application.

"scripts": {
    "start": "node index.js",
    "dev": "nodemon"
}

repository: It is an object which defines the URL where the source code is located, and what type of version control system it uses.

"repository": {
    "type": "git",
    "url": "https://github.com/john/example.git"
}

dependencies: It specifies the essential external packages or libraries upon which the project relies. These dependencies are vital for the proper functioning of the project and are presented as key-value pairs. The package name serves as the key, while the required version or version range is the corresponding value.

"dependencies": {
    "express": "^4.16.4",
    "compression": "~1.7.4"
}

devDependencies: It is similar to the dependencies field, but for packages that are only needed during development, and aren’t needed in production. The caret (^) and tilde (~) symbols found in the dependency versions indicate version range constraints following the Semantic Versioning (SemVer) standard.

"devDependencies": {
    "nodemon": "^1.18.11"
}

keywords: An array of keywords that describe the project.

"keywords": ["server", "frontend", "express", "compression"]

How to add dependencies?

Using npm as a package manager: Use this command to add dependencies to your project. you have to install the dependencies before using it in your project

npm install package-name --save

Using yarn as package manager: When using Yarn as your package manager, you can add a dependency with the following command, replacing “package-name” with the actual package name:

yarn add package-name

How to remove dependencies?

Using npm as package manager: Execute the following command so that you can remove that dependency from your project.

npm uninstall package-name

Using yarn as package manager: When using Yarn as your package manager, you can remove a dependency with the following command, replacing “package-name” with the actual package name:

yarn remove package-name

How to add a script?

Step 1: Open Your package.json File.

Step 2: Insert a key-value pair into the “scripts” object. The key serves as the identifier for your script, while the associated value is the command to be executed upon running the script.

"scripts": {
  "start": "node server.js",
  "test": "mocha test/*.js"
}

Step 3: Save the package.json File and run your script:

npm run start

or

npm run test

Conclusion: The package.json file serves as the core of every Node project, containing essential project metadata necessary for NPM publishing. It also specifies project functionality attributes that NPM utilizes for dependency installation, script execution, and identifying the package’s entry point. Although not all package.json fields may have relevance to your specific project, storing application information within this file can deliver substantial benefits.