Difference Between package.json and package-lock.json - Become A Better Programmer (2024)

When working on a project that utilizes NPM, you will find two files in your project root folder – the package.json file and the package-lock.json file. Most developers take note of only the former (package.json) since it’s where they write the scripts to start an application. However, what is the purpose of the two files? Why is “package-lock.json” longer? Which of these files can you do without?

The “package.json” file defines the rules required to run your application and install dependencies. On the other hand, the “package-lock.json” file holds detailed information on all the dependencies installed based on the package.json rules.

This post will guide you from scratch to better understand the two files. You will create an empty NodeJS project, build on it and see how each of these files is generated.

Table of Contents

Generate the Package.Json File

Tip: This step will require you to have NPM and NodeJS installed. Luckily, you don’t need to install the two packages separately. Download and Install NodeJS, which will automatically install NPM on your computer.

Create an empty folder anywhere you prefer on your Desktop and give it a name (say NodeAppOne). Launch the Terminal (or CMD for Windows OS) and navigate to the newly created folder. Execute the command below.

npm init

This command will give you a few prompts. You can leave most of these as they are, but for a better understanding of the “package.json” file, be sure to provide the “package name,” “description,” “author“, and “license.” Feel free to use some of the details shown in the image below.

Difference Between package.json and package-lock.json - Become A Better Programmer (1)

When done, you will see a prompt to save the configurations. Type “yes” and hit Enter.

Open the project folder (NodeAppOne) using any text editor like VSCode, Sublime Text, etc. You will see the generated “package.json” file with details similar to those shown in the image below. You will also notice that there is no “package-lock.json” file.

Difference Between package.json and package-lock.json - Become A Better Programmer (2)

When you open the “package.json” file, you will notice that it only shows you the general information of your application—nothing much interesting at this point.

Now, install several NPM packages since that’s where all the fun begins.

Install NPM Packages

For this post, you will install two packages. The first is a “required package,” and is necessary for your app to run in production. The other is a “development package,” which is not required in production. The reason for installing the two is to give you a clear understanding of the contents of the “package.json” file.

Execute the commands below.

npm install httpnpm install eslint --save-dev

When you return to your code editor, you will notice that two new entries have been generated – The “node modules” folder and the “package-lock.json” file.

Difference Between package.json and package-lock.json - Become A Better Programmer (3)

The “node modules” directory saves all the packages you download using NPM. However, this post won’t dive into that and will focus on the “package.json” and “package-lock.json” files.

The Package.Json File

If you followed each step described in this post, your “package.json” file should look similar to the image below.

Difference Between package.json and package-lock.json - Become A Better Programmer (4)

When you have an in-depth look at this file, you will notice that it contains all the meta information about your NodeJS application and version information of the installed packages. However, there are several properties this post would like you to take note of.

  • Name: This option specifies the name of your application.
  • Description: This option shows any additional information you provided about your application.
  • Main: This option specifies the Javascript file your Node application will use as the entry point. However, it’s an optional value and only required if your entry point differs from the “index.js” file in your project root folder.
  • Scripts: This section specifies your custom scripts executed with the NPM command. For example, if running a Next.JS application, you can add a line like "dev": "next dev". Now, when you run the npm run dev command, you are invoking "next dev"
  • Dependencies: This section lists all the installed packages required to run your application. You should see the “http” package we installed in the previous section.
  • DevDependencies: This section lists all the packages required by your application on “development” and not in “production.” These are packages that are installed using the --save-dev suffix.

Important: When you look at the dependencies listed in the “dependencies” and “devDependencies” sections, you will notice that they all have a caret symbol ^ in front of the version number. E.g "http": "^0.0.1-security"

Why is this caret symbol important, and why should you take note of it?

When committing your changes on a version control platform like Git, you don’t need to push the node modules folder. Now, if somebody downloads/clones your repository on their system, they will run the npm install command, which checks the “package.json” file, downloads all the required packages, and generates a node modules folder.

The caret symbol in the “package.json” file tells NPM that it can download a higher major version of the package if available. For example, if the version indicated in the “package.json” file is “1.3.5,” NPM can install a newer version of the package, say “1.9.3”, but never “2.0.0.”

So what’s the problem with that?

What could possibly go wrong with downloading an updated package/module?

In the best-case scenario, it sounds good because your application runs an updated module with better code optimization. However, this new release might contain bugs and vulnerabilities when you look at the worst-case scenario. Also, some packages get significant updates and changes that break your application—the worst nightmare for any developer.

To solve this issue, NPM introduced the “package-lock.json” file.

The Package-lock.json File

If you followed every step in this post, your “package-lock.json” should look similar to the image below.

Difference Between package.json and package-lock.json - Become A Better Programmer (5)

You will notice that this file is longer and contains much more information than the “package.json” file. For example, the “package.json” file in this post is only 17 lines, while the “package-lock.json” file has 2207 lines of code.

This file serves one primary purpose in your application. It locks the exact version of any dependency required by your application. Let’s understand that in-depth using an example.

This post installed the “http” module version “^0.0.1.” The “package-lock.json” file will lock this version such that even if you run the npm install command, NPM will check the “package-lock.json” file and install the exact “http” module version “0.0.1” even if there is a new release.

However, if you don’t have the “package-lock” file, NPM will use the “package.json” file and will end up installing the latest release of the “http” module.

Why is Package-lock.json Longer

This file not only locks the version of your module but also locks the version of sub-dependencies required by your module. For example, if “http” requires a module “xyz,” the “package-lock” file will also lock the version of “XYZ.”

In summary, this file locks the version of your dependency, the sub-dependency, the sub-sub-dependency, and so on.

Should you commit package-lock.json?

Up to this point, you now understand the significance of the “package-lock.json” file in your application. This post highly recommends that when you are committing your changes to Git, you don’t just push the “package.json” file but also commit the “package-lock.json” as it will save you a great deal in case of a new release of a module has bugs or vulnerabilities.

Sometimes you encounter an error in your NodeJS application, and the immediate solution has always been to delete the “package-lock.json” file. Since you now understand its significance in a Node project, always try other solutions before making that leap of faith.

Conclusion

This post has answered all the questions that you might have had concerning “package.json” and “package-lock” files. What is the purpose of the two files? Why is package-lock.json longer? Which of these files can you do without?

Although your application can run without the “package-lock.json” file, don’t cling to the notion “as long as it’s working.” It only takes one package update to break your application.

Was this article helpful?

You can also share your thoughts by replying on Twitter ofBecome A Better Programmeror tomy personal account.

How come there is a package.json and a package-lock.json in this project?

While they both share similar information, package-lock.json ensures your project installs specific versions of packages.

Learn more!https://t.co/tSG2wqZHzW

— Become A Better Programmer (@bbprogrammer) August 26, 2022
Difference Between package.json and package-lock.json - Become A Better Programmer (2024)

FAQs

Difference Between package.json and package-lock.json - Become A Better Programmer? ›

json specifies version ranges for dependencies, but these ranges can sometimes be broad, allowing for incompatible updates. package-lock. json locks down exact versions, preventing accidental upgrades that could introduce breaking changes.

What is the difference between package lock json and package json? ›

While package. json sets the stage with dependency listings and version ranges defined through semantic versioning or specific numbers, package-lock. json goes a step further by resolving and documenting the precise dependencies, sub-dependencies, and installation paths for accurate reproducibility.

Should I commit both package json and package lock json? ›

Should package lock JSON be committed? Yes it must be committed as it ensures no any dependency breaks your app. package-lock. json contains the exact version your application is supposed to use where as package.

Why should I not modify package lock json? ›

To ensure consistency and reproducibility across different environments, both files should be committed to version control, and developers should be careful not to modify the package-lock. json file directly.

What is the purpose of npm package lock json? ›

This file is intended to be committed into source repositories, and serves various purposes: Describe a single representation of a dependency tree such that teammates, deployments, and continuous integration are guaranteed to install exactly the same dependencies.

What is package json main used for? ›

Your package. json holds important information about the project. It contains human-readable metadata about the project (like the project name and description) as well as functional metadata like the package version number and a list of dependencies required by the application.

Should package-lock json be deployed? ›

Some disagreement exists on whether repos should include package-lock. json , but committing it does ensure greater consistency between the environments where it was created and the Netlify Build environment, so it is beneficial to commit it and push it, we think.

Do we need to ignore package lock in json? ›

It is highly recommended you commit the generated package lock to source control: this will allow anyone else on your team, your deployments, your CI/continuous integration, and anyone else who runs npm install in your package source to get the exact same dependency tree that you were developing on.

Can I delete package lock json and run npm install? ›

json appears in your project directory. Don't delete that package-lock file, run npm install and regenerate it! package-lock. json, a file generated by npm since v5 was released in 2017, does what its name suggests: helps lock package dependencies down, as well as their sub-dependencies.

What is the benefit of package json? ›

Using scripts in package. json offers several benefits: Simplifies tasks: Scripts can automate complex commands, making it easier for users to perform tasks. Improves user experience: Scripts can provide a more intuitive interface for users, hiding complex commands and dependencies.

Can we manually update package-lock json? ›

To update all the dependencies to the latest version and package. json and package-lock. json, you can use the command "npm update --save" or "npm update --save-dev" After updating the dependencies, you should verify that your project still works as expected and fix any issues that the updates may have introduced.

Why does package-lock.json keep changing? ›

When you run npm install , npm reads the dependencies listed in your package. json file and installs the specified versions. The package-lock. json file is updated to reflect the exact versions of each dependency and its transitive dependencies.

Is package-lock json updated automatically? ›

json , is package-lock. json . This is a file that npm creates and updates automatically every time you run npm install (or npm i ). It tracks the exact versions of packages and their dependencies that are installed in your project.

What is the difference between package lock and package json? ›

While package. json acts as the project's manifest, detailing dependencies and scripts, package-lock. json ensures consistent and reliable installation of these dependencies across different environments.

Should package lock.json be committed? ›

It aids cross environmental collaboration, where everyone fetches dependencies from the same tree. In addition, by committing the package-lock file, you can go back in history and replicate the exact dependency tree from that time.

How to remove unused packages from package lock in npm? ›

You can use npm-prune to remove extraneous packages.

This command removes "extraneous" packages. If a package name is provided, then only packages matching one of the supplied names are removed. Extraneous packages are packages that are not listed on the parent package's dependencies list.

How to generate package-lock json and package json? ›

Step-by-step Guide to Generate package-lock. json Files
  1. In your project's root directory, open a command line or terminal.
  2. Verify that you have a package. ...
  3. Run the command npm install or yarn install to install all the dependencies specified in the package. ...
  4. After the installation process completes, a package-lock.
Apr 19, 2024

What is the difference between package-lock json and Yarn lock? ›

In Yarn, it is called yarn. lock while in npm, it is called package-lock. json. As the name implies, this file locks the dependencies to their stipulated versions during the installation process, after establishing the versioning parameters in the package.

Can I delete package-lock json and run npm install? ›

json appears in your project directory. Don't delete that package-lock file, run npm install and regenerate it! package-lock. json, a file generated by npm since v5 was released in 2017, does what its name suggests: helps lock package dependencies down, as well as their sub-dependencies.

How is npm shrinkwrap json different than package-lock json? ›

The difference is that package-lock. json cannot be published, and it will be ignored if found in any place other than the root project. In contrast, npm-shrinkwrap. json allows publication, and defines the dependency tree from the point encountered.

Top Articles
Charged for using contactless - Google Wallet Community
What Is Open Interest & Why Is It Important? | Paytm Money Blog
Www.mytotalrewards/Rtx
Diario Las Americas Rentas Hialeah
Fully Enclosed IP20 Interface Modules To Ensure Safety In Industrial Environment
What Auto Parts Stores Are Open
Victoria Secret Comenity Easy Pay
Tlc Africa Deaths 2021
PGA of America leaving Palm Beach Gardens for Frisco, Texas
Fredericksburg Free Lance Star Obituaries
The Murdoch succession drama kicks off this week. Here's everything you need to know
Directions To O'reilly's Near Me
Gino Jennings Live Stream Today
Bridge.trihealth
623-250-6295
No Hard Feelings - Stream: Jetzt Film online anschauen
Craigslist Pinellas County Rentals
Moving Sales Craigslist
Quick Answer: When Is The Zellwood Corn Festival - BikeHike
How to Grow and Care for Four O'Clock Plants
27 Paul Rudd Memes to Get You Through the Week
Apartments / Housing For Rent near Lake Placid, FL - craigslist
Ltg Speech Copy Paste
Synergy Grand Rapids Public Schools
Labcorp.leavepro.com
Cornedbeefapproved
Little Einsteins Transcript
Evil Dead Rise - Everything You Need To Know
+18886727547
134 Paige St. Owego Ny
Inmate Search Disclaimer – Sheriff
Learn4Good Job Posting
Wasmo Link Telegram
Bt33Nhn
Ourhotwifes
Gwu Apps
Admissions - New York Conservatory for Dramatic Arts
Is Arnold Swansinger Married
ENDOCRINOLOGY-PSR in Lewes, DE for Beebe Healthcare
Jail View Sumter
NHL training camps open with Swayman's status with the Bruins among the many questions
Stanley Steemer Johnson City Tn
Bones And All Showtimes Near Johnstown Movieplex
Gfs Ordering Online
Scythe Banned Combos
Enr 2100
Sandra Sancc
Booknet.com Contract Marriage 2
5103 Liberty Ave, North Bergen, NJ 07047 - MLS 240018284 - Coldwell Banker
Unit 4 + 2 - Concrete and Clay: The Complete Recordings 1964-1969 - Album Review
Honeybee: Classification, Morphology, Types, and Lifecycle
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 6732

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.