Open In App

Difference between package.json and package-lock.json files

In this article, we will learn the major differences between package.json and package.lock.json and their needs in Node.

In Node, package.json is a versioning file used to install multiple packages in your project. As you initialize your node application, you will see three files installed in your app that is node_modules, package.json, and package.lock.json.



You can initialize the node project by running the below command:

npm init

The Role of package.json:

1. Project Configuration:



2. Dependency Management:

3. Version Management:

After initializing, your package.json will look something like this:

{
"name": "Your project name",
"version": "1.0.0",
"description": "Your project description",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
},
"author": "Author name",
"license": "ISC",
"dependencies": {
"dependency1": "^1.4.0",
"dependency2": "^1.5.2"
}
}

As we can see above, a package.json file contains metadata about the project and also the functional dependencies that is required by the application.

The Role of package-lock.json:

1. Dependency Locking:

2. Version Consistency:

3. Improved Installation Speed:

Below is how a typical package-lock.json file looks:

{
"name": "Your project name",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"dependency1": {
"version": "1.4.0",
"resolved":
"https://registry.npmjs.org/dependency1/-/dependency1-1.4.0.tgz",
"integrity":
"sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA=="
},
"dependency2": {
"version": "1.5.2",
"resolved":
"https://registry.npmjs.org/dependency2/-/dependency2-1.5.2.tgz",
"integrity":
"sha512-WOn21V8AhyE1QqVfPIVxe3tupJacq1xGkPTB4iagT6o+P2cAgEOOwIxMftr4+ZCTI6d551ij9j61DFr0nsP2uQ=="
}
}
}

`package-lock.json` is crucial for locking dependencies to specific versions, ensuring consistent installations across different environments. Without it, variations in installed versions may occur. This file guarantees reproducibility by specifying exact versions, preventing discrepancies. Including both `package.json` and `package-lock.json` in source control ensures that collaborators install the exact dependencies, maintaining uniformity.

Difference between package.json & package-lock.json:

package.json

package.lock.json

It contains basic information about the project. It describes the exact tree that was generated to allow subsequent installs to have the identical tree.
It is mandatory for every project. It is automatically generated for those operations where npm modifies either node_modules tree or package.json.
It records important metadata about the project. It allows future devs to install the same dependencies in the project.
It contains information such as name, description, author, script, and dependencies. It contains the name, dependencies, and locked version of the project. 
Article Tags :