package.json vs package-lock.json: Major Differences
Updated on May 20, 2025 | 22 min read | 17.37K+ views
Share:
For working professionals
For fresh graduates
More
Updated on May 20, 2025 | 22 min read | 17.37K+ views
Share:
Table of Contents
Did you know? npm v7 supercharged package-lock.json! It now captures the entire dependency tree and automatically resolves peer dependencies, making installs faster and more reliable. Perfect for CI/CD with npm ci ensuring deterministic builds every time! |
In Node.js, package.json and package-lock.json are both essential files for managing dependencies, but they serve distinct purposes. While package.json defines the project's metadata, dependencies, and scripts, package-lock.json locks the exact versions of these dependencies to ensure consistent and reliable installations across different environments.
In this blog, we'll explore the differences between package.json and package-lock.json, their roles in dependency management, and best practices for using both JSON files to ensure stable, consistent builds.
Want to dive deeper into JavaScript and Node.js? Explore upGrad's Comprehensive Software Development Courses to build robust, scalable applications and enhance your development skills. Join today!
The package.json file defines your project's dependencies, scripts, and metadata, providing an overview of your project's configuration. In contrast, package-lock.json ensures that every environment installs the exact same dependencies by locking the versions.
To meet the industry's rising need for skilled professionals, it's important to continuously improve your skills. Check out these top courses and take your expertise to the next level.
In this section, we'll explore how these files interact, their unique purposes, and why both are crucial for maintaining consistent and reliable builds.
Parameter | package.json | package-lock.json |
Definition | The package.json file is the core of any node project. It stores critical metadata about a project that is necessary before releasing IT to npm, and it also specifies the functional properties of a project that npm utilizes. | The package-lock.json is a lockfile that holds information on the dependencies or packages installed for a node.js project, including their exact version numbers. |
Purpose | The package for your project. json is the primary format for configuring and describing how to communicate with and execute your application. The npm CLI utilizes it to distinguish your project and comprehend how to handle its dependencies. | Its objective is to offer an immutable version of package.json, so you may fetch an earlier version of your code and end up with the same node_modules folder. |
Properties | Package.json has the following properties: The name property: defines the name of the package The version property: states the current version of the software The license property: states what the license implies to the entire code The description property states the package on the website and in search results. The keywords property: assist in finding packages The dependency property: an object that contains package names and a version or version range |
Package-lock.json has the following properties: The name property: states the package's name The locked version property: states the locked version of the package The dependency property: states the dependency of the package |
Store | More than just dependencies, it also establishes a project's attributes, descriptions, and license information. | It is just used to lock dependencies. |
How to Create | It can be created in two ways: 1. Using npm init Syntax: npm init 2. Writing directly: write into the file with all the necessary information |
npm install automatically creates a package-lock.json: npm install Pass the —package-lock-only option to build a lock file: npm install —package-lock-only Alternatively, use the option —package-lock: npm install --package-lock |
Record Versions | It stores the minimum version required by your program. If you upgrade the versions of a certain package, the change will not be shown here. | Keeps track of the exact version of each installed package, allowing you to reinstall it. Future installations will be able to generate the same dependency tree. |
Requisite | It is requisite for every project. | It is generated automatically whenever npm alters the modules tree or package.json. |
Located | The package.json file is located in the root directory of the project. | It is located in the node_modules tree or package.json. |
Disability Feature | It is not possible. package.json is the core directory of the project. | It is possible. Set package-lock=false in /.npmrc to instruct the npm command not to create package-lock.json. |
Also Read: Top 20 Programming Languages of the Future
After understanding the difference between package.json and package-lock.json, let’s discuss the specifics of package.json and how it helps define your project's structure and dependencies.
package.json is a key file in Node.js projects that contains essential information about the project and its dependencies. It is crucial for the project’s configuration and management.
This is an illustration of a check npm version packaging. A sample json file may look like this:
{
"name": "my-project",
"version": "1.5.0",
"description": "Express server project using compression",
"main": "src/index.js",
"scripts": {
"start": "node index.js",
"dev": "nodemon",
"lint": "eslint **/*.js"
},
"dependencies": {
"express": "^4.16.4",
"compression": "~1.7.4"
},
"devDependencies": {
"eslint": "^5.16.0",
"nodemon": "^1.18.11"
},
"repository": {
"type": "git",
"url": "https://github.com/osiolabs/example.git"
},
"author": "Jon Church",
"contributors": [{
"name": "Amber Matz",
"email": "example@example.com",
"url": "https://www.osiolabs.com/#team"
}],
"keywords": ["server", "osiolabs", "express", "compression"]
}
Also Read: 10 Practical Applications of JavaScript And Career Tips
package.json is the heart of any Node.js project, acting as a bundle that contains essential metadata and configuration data for your application. It allows npm (and yarn) to identify and manage your project's dependencies efficiently. Here's how it plays a critical role:
NPM CLI Integration: When you use npm (or yarn), the CLI looks for package.json to figure out which dependencies to install, run scripts, and execute commands like npm install.
Tracking and Updating: It helps you easily keep track of your project's dependencies and version updates, saving you from confusion when managing packages.
Core Requirement: Even if you’re not publishing your project to the NPM registry, package.json is still essential. You can’t install or check dependencies using npm without it.
Project Consistency: Ensures all contributors use the same versions of dependencies, preventing inconsistencies and potential bugs across environments.
Key Fields in package.json
Here are some essential fields you'll commonly find in a package.json file:
1. name:
2. version:
3. license:
Also Read: Top 25+ JavaScript Frameworks to Learn in 2025 & How to Choose the Right One
Having explored the contents of package.json, we now turn our attention to package-lock.json, which complements it by locking the precise dependency versions to maintain stability in your project.
package-lock.json is a file automatically generated when you install dependencies using npm (Node Package Manager) version 5 or higher. It locks the specific versions of dependencies to ensure consistency across environments.
Semantic versioning (or semver) is essential for understanding the lockfile and package.json in npm. It plays a key role in making npm successful by ensuring that dependencies are managed intelligently. By checking the npm version, you can gain insight into how this system works.
When developing an application that interacts with other third-party apps, it’s important to explain how any modifications you make will affect their ability to integrate with your app. This is where semantic versioning comes into play.
There are three core components to semantic versioning:
For example, 1.2.3 means:
Changes in the major version often break compatibility, so users need to adapt. Minor version upgrades typically add features without causing issues, while patch updates are simple fixes.
The primary goal of npm is to simplify package management. Your project might have hundreds of dependencies, and each of those could rely on even more dependencies. npm’s purpose is to free you from worrying about discrepancies between these dependencies, offering easy commands to install and manage them.
When you install a package via npm, an entry is added to your package.json file. This ensures that your project’s dependencies are properly tracked.
Additionally, npm supports the use of wildcards in version specifications. For instance, if you specify a version like "1.2.12", npm will install at least version 1.2.12, but it’s okay to use any higher version that maintains the same major version number (in this case, 1).
To avoid potential issues with dependencies, npm uses a package lock. This lock ensures that the same modules are installed every time, as defined by your package.json.
When you use npm package-lock version 5.x.x or higher, npm automatically generates a package-lock.json file to secure the exact versions of dependencies you’re using. You can deactivate this feature if needed, but it’s crucial for maintaining consistency in your project’s dependencies.
Also Read: Web Application Architecture: Function, Components, Types & Real Life Examples
With a clearer understanding of package-lock.json and its function, let's discuss the key differences between it and package.json, which often causes confusion among developers.
Both package-lock.json and package.json are essential for managing dependencies in your Node.js projects, but they serve different purposes and contain distinct information. Let’s dive into how package-lock.json differs and why it’s crucial for maintaining consistency across your project.
1. The Structure: Similar, Yet Different
At first glance, both package-lock.json and package.json might appear to have a similar structure. They contain information about your project's dependencies, but package-lock.json goes much further by providing exact versions and a more comprehensive list of dependencies.
For example, when you check Express in package.json, you’ll only see the name and version of the library:
"express": "^4.16.3"
However, in package-lock.json, you’ll see much more detail, like the exact version being used, as well as nested dependencies:
"express": {
"version": "4.16.3",
"resolved": "https://registry.npmjs.org/express/-/express-4.16.3.tgz",
"integrity": "sha512-xxxxxx",
"dev": false,
"engines": { "node": ">=0.10.0" },
"dependencies": {
"body-parser": "^1.18.2",
"array-flatten": "2.1.1"
}
}
Package-lock.json locks down these exact details, ensuring that no matter who installs the project or when they do so, they always get the same version of the dependencies.
2. The Tilde (~) and Exact Versioning
In package.json, you might specify a version range for your dependencies. For instance, using a tilde (~) means npm can install any patch version:
"express": "~4.16.3"
This means npm can install 4.16.3, 4.16.4, or even 4.16.5. This flexibility might lead to discrepancies in environments as different machines could install slightly different patch versions.
In package-lock.json, however, the exact version is locked:
"express": "4.16.3"
No surprises here! This precise versioning ensures that all contributors to the project, regardless of environment, are using the exact same version, eliminating potential issues with inconsistent builds.
3. Why is This Important?
Imagine you specify an older version of a framework like 4.16.3 in your package.json:
"express": "4.16.3"
This version might still be fine for security patches, but the package-lock.json ensures that everyone working on the project installs the exact same version. This locks down the environment, preventing any future discrepancies.
In a scenario where package-lock.json is absent, and a new developer installs dependencies, they could accidentally install a newer or different patch version that might break the application. However, with package-lock.json present, the exact same version is always installed.
4. Dependencies Within Dependencies
Here’s an important point: libraries like Express often have their own dependencies. These secondary dependencies are nested within the primary one.
For example:
Also Read: Is Web Development a Good Career Option in 2025?
Having clarified the differences between package.json and package-lock.json, you’re one step closer to mastering software development. Let’s explore how upGrad can help you accelerate your expertise!
The distinction between package.json and package-lock.json is essential for efficient dependency management in Node.js. While package.json defines project metadata and dependencies, package-lock.json locks in exact versions of packages, ensuring consistent and reliable builds across different environments.
If you're looking to advance your career in tech, upGrad’s programs in Cloud Computing, DevOps, and Full-Stack Development offer hands-on experience and mentorship. With real-world projects and personalized guidance, you can bridge skill gaps and unlock new career opportunities.
To further boost your skills, explore these additional free courses and enhance your learning experience:
You can contact upGrad’s expert career counselors, who will guide you based on your goals. You can also visit a nearby upGrad offline center to explore course options, get hands-on experience, and speak directly with mentors!
Boost your career with our popular Software Engineering courses, offering hands-on training and expert guidance to turn you into a skilled software developer.
Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.
Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.
References:
https://docs.npmjs.com/cli/v8/configuring-npm/package-lock-json
https://blog.npmjs.org/post/621733939456933888/npm-v7-series-why-keep-package-lockjson.html
900 articles published
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources