View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

package.json vs package-lock.json: Major Differences

By Pavan Vadapalli

Updated on May 20, 2025 | 22 min read | 17.37K+ views

Share:

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!

package.json vs package-lock.json

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.

Coverage of AWS, Microsoft Azure and GCP services

Certification8 Months

Job-Linked Program

Bootcamp36 Weeks

Master essential tools like package.json and package-lock.json to manage dependencies efficiently and streamline your development process. Join upGrad’s Full Stack Development Bootcamp, where you’ll gain hands-on experience and personalized mentorship to elevate your career!

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.

Understanding package.json

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.

  • Metadata: Project name, description, version, author, and license.
  • Dependencies: Lists the libraries and packages your project relies on.
  • Scripts: Defines custom scripts like start, build, test, etc.
  • Engines: Specifies which versions of Node.js and npm are compatible.

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:

  • This field defines the name of your project or package. It is used to identify your project in the NPM registry.
  • Tip: Ensure that your project name is unique when publishing to NPM.

2. version:

  • Specifies the current version of your project (e.g., "1.5.0").
  • Semantic Versioning (SemVer): The version follows the format major.minor.patch and helps indicate the level of changes to your project.
    • Major: Introduces breaking changes.
    • Minor: Adds new features without breaking the existing functionality.
    • Patch: Includes bug fixes or small updates.

3. license:

  • Defines the licensing information for your project (e.g., "MIT", "ISC").
  • Why It’s Important: It specifies how others can use, modify, and distribute your code. It's a crucial field if you plan to share your code publicly or collaborate with others.

Learn the essential concepts of JavaScript and kickstart your programming career by building dynamic web applications. Start with upGrad’s JavaScript Basics from Scratch free course today!

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.

What is package-lock.json?

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.

  • Dependency Locking: It locks the exact versions of all installed packages, ensuring that everyone working on the project gets the same version of dependencies.
  • Ensures Consistency: Even if a dependency gets updated later, package-lock.json ensures that the version specified in the file is used.
  • Faster Installs: Since the exact package versions are locked, npm can install dependencies more quickly by skipping the version resolution step.
  • Automatic Generation: This file is automatically created and updated when you run npm install.

Why is package-lock.json Important?

  • Prevent Missing or Incorrect Versions: Without package-lock.json, different environments may end up with different versions of dependencies, potentially leading to bugs or errors.
  • Works with package.json: While package.json lists dependencies, package-lock.json ensures that the project consistently installs the correct versions of those dependencies.
  • Important for Team Collaboration: If you're working on a team, everyone needs the same exact dependency versions to avoid "it works on my machine" issues.

Common Issues and Mistakes

  • Deleting package-lock.json: Deleting this file and running npm install can cause npm to generate a new lock file, which may not match your previous environment.
  • Not Committing package-lock.json: If you're using version control (e.g., Git), failing to commit the package-lock.json can cause inconsistencies between environments.

1. Versioning with Semantics

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:

  • Major version (X): Indicates significant changes that may break compatibility.
  • Minor version (Y): Adds new features without breaking existing functionality.
  • Patch version (Z): Represents bug fixes that don’t affect compatibility.

For example, 1.2.3 means:

  • 1 is the major version
  • 2 is the minor version
  • 3 is the patch version

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.

2. Keeping Track of What You Have

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).

3. The Endgame: Locking It Down

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.

Take the next step in your AI and ML journey with upGrad’s Executive Diploma in Machine Learning and AI. Gain practical skills and knowledge to excel in top roles like AI Engineer and Machine Learning Specialist. Enroll now!

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.

Difference Between package.json and package-lock.json

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:

  • Express may bring in libraries like array-flatten, body-parser, and others.
  • Package-lock.json will track all of these nested dependencies and their exact versions, making sure that every dependency used by Express (and in turn, your app) is locked to the precise version needed.

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!

Become an Expert in Software Development with upGrad!

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

Frequently Asked Questions (FAQs)

1. What is the purpose of the package.json file in Node.js projects?

2. How does package-lock.json differ from package.json?

3. Can I use npm install without package-lock.json?

4. Why is it important to commit package-lock.json to version control?

5. How does package-lock.json help in continuous integration (CI)?

6. Can I manually edit the package-lock.json file?

7. Does npm install --package-lock-only create a new package-lock.json without installing dependencies?

8. What happens if I delete package-lock.json and run npm install?

9. How does npm handle peer dependencies in package-lock.json?

10. Can package.json and package-lock.json become out of sync?

11. What is the impact of using npm ci instead of npm install?

Pavan Vadapalli

900 articles published

Get Free Consultation

+91

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

View Program

Top Resources

Recommended Programs

upGrad

AWS | upGrad KnowledgeHut

AWS Certified Solutions Architect - Associate Training (SAA-C03)

69 Cloud Lab Simulations

Certification

32-Hr Training by Dustin Brimberry

upGrad

Microsoft | upGrad KnowledgeHut

Microsoft Azure Data Engineering Certification

Access Digital Learning Library

Certification

45 Hrs Live Expert-Led Training

upGrad

upGrad KnowledgeHut

Professional Certificate Program in UI/UX Design & Design Thinking

#1 Course for UI/UX Designers

Bootcamp

3 Months