Introduction
When working with Node.js, you often install packages using npm (Node Package Manager). After installing packages, you may notice a file named package-lock.json in your project folder.
Many beginners wonder why this file is created, what information it stores, and whether they should upload it to GitHub.
The package-lock.json file plays an important role in managing project dependencies. It records the exact versions of installed packages and their dependency information so that the project can be installed consistently across different environments.
What Is package-lock.json in Node.js?
The package-lock.json file is automatically generated and maintained by npm. It records the exact dependency versions and the dependency tree used by a Node.js project.
For example, suppose you install Express in your project using the following command:
npm install express
npm installs Express and its required dependencies. It also creates or updates the package-lock.json file to record details about the installed packages.
This helps npm install the same dependency versions when the project is set up on another computer, provided the lockfile and installation environment are compatible.
In simple words: package.json describes the packages your project needs, while package-lock.json records the specific dependency versions npm resolved.
Why Is package-lock.json Used?
The package-lock.json file is useful for several reasons.
1. Keeps Dependency Versions Consistent
It records the exact versions of packages in the dependency tree. This helps prevent unexpected changes when another developer installs the project.
2. Supports Reproducible Installations
Developers can use the lockfile to install the same resolved dependency tree across development, testing, and production environments.
3. Helps Avoid Unexpected Updates
Package version ranges in package.json may allow newer compatible versions to be installed. The lockfile records the versions npm selected, reducing unexpected changes during installation.
4. Improves Installation Efficiency
npm can use the lockfile to determine which packages and versions are needed, avoiding some of the work involved in resolving dependencies from scratch.
5. Helps Teams Collaborate
When developers share a project through Git, committing package-lock.json helps team members install a consistent set of dependencies.
What Is the Difference Between package.json and package-lock.json?
Both files are important in Node.js projects, but they serve different purposes.
| Feature | package.json | package-lock.json |
|---|---|---|
| Main purpose | Describes project details and dependencies. | Records the resolved dependency tree. |
| Created by | Usually created with npm init or maintained by developers and tools. |
Generated and updated by npm. |
| Dependency versions | May contain version ranges. | Records exact resolved versions. |
| Scripts | Stores commands such as start and test. |
Does not define project scripts. |
| Dependency details | Lists direct dependencies and development dependencies. | Includes detailed information about resolved packages and their dependencies. |
| Should be committed to Git? | Yes, normally. | Yes, normally. |
For example, your package.json might contain:
{
"dependencies": {
"express": "^5.1.0"
}
}
The ^ symbol allows npm to select compatible versions within the specified version range. The actual resolved version is recorded in package-lock.json.
The lockfile also contains information about the packages that Express depends on.
Note: The example version is illustrative. The actual version depends on the package version available and your project’s dependency resolution.
How Is package-lock.json Created?
You usually do not need to create this file manually. npm generates it when you install packages or otherwise resolve project dependencies.
Follow these steps to understand the process.
Step 1: Create a Node.js Project
Open your terminal and run:
mkdir node-lock-demo
cd node-lock-demo
npm init -y
The npm init -y command creates a package.json file with default values.
Step 2: Install a Package
Install Express:
npm install express
npm installs Express and its dependencies. It also creates or updates these files:
- package.json
- package-lock.json
- node_modules/
Step 3: Check Your Project Files
Your project structure should look similar to this:
node-lock-demo/
├── node_modules/
├── package.json
└── package-lock.json
The node_modules directory contains the installed packages. The package.json file describes the project and its dependencies, while package-lock.json records the resolved dependency tree.
Understanding the Structure of package-lock.json
The package-lock.json file contains structured JSON data. Its exact contents depend on the npm version and the packages installed.
A simplified example looks like this:
{
"name": "node-lock-demo",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "node-lock-demo",
"version": "1.0.0",
"dependencies": {
"express": "^5.1.0"
}
},
"node_modules/express": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/express/-/express-5.1.0.tgz",
"integrity": "sha512-example"
}
}
}
This is an illustrative example, not a complete real lockfile. The integrity value shown is a placeholder.
Let’s understand the important fields.
| Field | Description |
|---|---|
name |
The name of the project or package. |
version |
The project or package version. |
lockfileVersion |
Identifies the lockfile format version. |
requires |
A legacy field that may appear in lockfiles. |
packages |
Contains information about the root project and resolved packages. |
dependencies |
Lists dependency requirements for the project or a package. |
resolved |
Identifies the package source, often a registry tarball URL. |
integrity |
Provides a cryptographic integrity value used to verify package content. |
The packages section is particularly important because it records details about the packages installed in the dependency tree.
How Does package-lock.json Work?
Let’s understand how the lockfile works during a typical development workflow.
1. Installing a Package
When you run:
npm install express
npm resolves the required packages, installs them, and updates package.json and package-lock.json as needed.
2. Sharing the Project
You commit your project files, including package.json and package-lock.json, to a Git repository.
Another developer can clone the project and run:
npm install
npm uses the available lockfile to guide dependency installation.
3. Installing Dependencies in CI or Production
For automated builds where you want to install exactly the dependency tree recorded in the lockfile, you can use:
npm ci
The npm ci command requires a lockfile that matches package.json. It removes an existing node_modules directory before installing dependencies and does not update the lockfile.
This makes npm ci particularly useful in continuous integration (CI) workflows.
What Is the Difference Between npm install and npm ci?
Both commands install dependencies, but they are designed for different workflows.
| Feature | npm install | npm ci |
|---|---|---|
| Main purpose | Install or update dependencies. | Perform a clean, lockfile-based installation. |
| Requires a lockfile | No. | Yes. |
| Updates the lockfile | May update it. | Does not update it. |
Existing node_modules |
Reuses or updates it as needed. | Removes it before installation. |
| Common use | Local development. | CI builds and repeatable deployments. |
Example:
Use npm install when adding a new package or changing dependencies during development.
Use npm ci when setting up a clean environment using an existing, valid lockfile.
Should You Commit package-lock.json to Git?
Yes. For most Node.js applications, you should commit package-lock.json to your Git repository.
Including the lockfile helps developers and automated build systems install a consistent dependency tree.
A typical project should include:
my-node-project/
├── node_modules/
├── package.json
├── package-lock.json
└── app.js
Usually, you commit package.json and package-lock.json, but add node_modules/ to .gitignore because it can be regenerated by installing dependencies.
Example .gitignore entry:
node_modules/
Do not normally ignore package-lock.json for an application project.
Common Mistakes When Using package-lock.json
Beginners sometimes make the following mistakes.
1. Deleting package-lock.json Without a Reason
Deleting the lockfile can cause npm to resolve different dependency versions within the ranges allowed by package.json.
Solution: Keep the file unless you have a specific reason to regenerate it.
2. Not Committing the Lockfile
If developers do not share the lockfile, each environment may resolve dependencies differently.
Solution: Commit package-lock.json to Git for most application projects.
3. Editing the Lockfile Manually
Changing package versions or integrity values manually can make the lockfile inconsistent or invalid.
Solution: Use npm commands to manage dependencies instead of editing the file directly.
4. Running npm ci Without a Valid Lockfile
The npm ci command fails if the lockfile is missing or does not match package.json.
Solution: Run npm install to resolve and synchronize the dependencies, review the changes, and commit the updated files.
5. Confusing package-lock.json With node_modules
These files serve different purposes. The lockfile records dependency information, while node_modules contains the installed package files.
Solution: Keep the lockfile in version control and normally exclude node_modules/.
Conclusion
The package-lock.json file is an important part of dependency management in Node.js. It records the exact resolved versions of packages and helps maintain consistent installations across development machines and deployment environments.
For most Node.js applications, keep both package.json and package-lock.json in version control, avoid editing the lockfile manually, and use npm ci when you need a clean installation based on the committed lockfile.