npm install and the package-lock.json file (2024)

There’s something that lives in many modern development projects, sleeping quietly in the root of the project, this little file package-lock.json. Most of us treat it with discontent, and ignore it completely, or at best, delete it every once in a while cause some stack overflow or blog post told us to. There are about 57,000 pages on Google that talk about deleting package-lock.json. Why does that work and what does it do? If we can just delete it, why even have it?

What is the package-lock.json file?

Living in the root of your project right next to its big brother, package.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. Go take a peek at yours. You’ll see every library and package, every one, listed there. You’ll see some you didn’t install. The package-lock.json file tracks every package your app uses, and packages your packages use, all the way down the tree!

If you’re curious to see who’s using a specific package, try npm list packagename and it’ll list the tree for that package.

npm list [email protected] /Users/david.lozzi/git/project├─┬ @babel/[email protected]│ └─┬ @babel/[email protected]│ └─┬ @babel/[email protected]│ └── [email protected] deduped└─┬ [email protected] └─┬ [email protected] └── [email protected] 

The package-lock.json file ensures that everyone working on the project has the same versions of packages, which is crucial for avoiding conflicts and unexpected behavior. This should remove some of the “it works on my machine” when our test or production environments act differently.

If you’re wondering, yes, this file is important and should be kept, and included in your code repository. Make sure this is under version control with the rest of your project files. Should we delete it? Read on…

Using npm install

As you know, npm i is used to install packages from the Node Package Manager (npm). When you run npm i <package-name>, npm downloads the package from the registry and installs it in your project’s node_modules directory. (this folder should not be version controlled, make sure it’s in your .gitignore file, and especially not synced with the cloud, like through OneDrive. I did that once, and it hurt… a lot.)

After the package is downloaded, the package-lock.json file is then updated with the exact version that was downloaded. Why? Well, your package.json (yea, with no lock) doesn’t always track the exact version of a package. Take this example:

 "js-cookie": "2.2.1", "prop-types": "~15.7.2", "react": "^17.0.0", "react-redux": ">=8.0.0",

What this will do:

  • Install js-cookie at version 2.2.1, always at that version
  • Install prop-types at version 15.7.x, getting the latest patch version
  • Install react at version 17.x.x, getting the latest minor and patch version
  • Install react-redux with the latest version that’s greater than or equal to version 8.0.0

As you may surmise if a team of developers did their own npm install at varying times, they could all get different versions! And with each of them running npm install, they’re all updating the package-lock.json file, and now the others have to npm i to match versions (and may inadvertently update package-lock.json again). It can be a vicious cycle…

npm install and the package-lock.json file (1)

In your local development environments, this is actually okay. You want to work with the latest libraries, and if you really don’t, then change your package.json file to limit the versions. Npm uses the ^ prefix on versions as a default, allowing minor versions through. Edit your package accordingly to limit the version. I don’t suggest forcing them all to be a specific version by removing the prefix. Getting most patches and minor versions is a good thing: folks managing these packages will release fixes, security patches, etc. that will improve your app. You’ll get the latest version the next time you do an npm i. According to best practices of using version numbering (see semver), patches and minor updates should not cause breaks… in theory.

It’s also worth noting that when you do an npm i with a package-lock.json file, the install process does refer to the lock file to check versions (among other things). If the packages and versions listed in the package-lock.json file match the dependencies specified in the package.json file, npm will simply install those packages without making any changes.

You may want to delete the package-lock.json file, if needed, to clear npm’s head a little, and let it go out to the wild and get everything it needs. If you do delete it, make sure to do an npm i right after to recreate it. Keep in mind, that lock file will now have all of the latest packages (per the version defined in package.json). Again, this should be okay and is recommended. Make sure you keep that lock file in your repo, ’cause you’ll need it for the next part.

What is npm ci?

Given the above, you might start wondering “Why have a package-lock.json file to begin with? It’s always being updated and I can just delete it!”. I’d agree. Why bother? There must be a way to rely on this file to guarantee the same versions will be installed again and again…

Enter npm ci!

npm ci is a more strict and reliable version of npm install. I’m not entirely sure what ci stands for, many think it stands for Continuous Integration. This isn’t a terrible definition as npm ci is designed to be used in your automation, test, build, and/or deployment pipelines. The primary difference between npm ci and npm install is that npm ci requires an existing package-lock.json file and installs the exact versions of packages specified in that file.

npm install will refer to and update package-lock.json as needed.

npm ci will rely on the package-lock file to determine which versions to install. It will not update the package-lock.json file.

Pretty straightforward. Also, because it’s reading the lock file, npm ci tends to be faster, there’s no discovery or negotiating on what version to pull, it just reads the file. I saw a 60-80s install in my pipeline drop to about 20-30s after switching to npm ci.

When should you use npm install vs. npm ci?

I recommend using npm install during development when you are actively working on the project and installing new packages. Your dev team should always use npm install. Make sure package-lock.json is in your code repo.

Use npm ci in your automation and build pipelines to ensure that the exact same versions of packages are installed in every environment. This will guarantee consistency and removes the possibility of version conflicts or unexpected behavior. Also, it’ll speed up your pipeline a little bit too.

npm install for developers.

npm ci for pipelines.

Good? Good.

What did I miss? Any tips you’d suggest for using npm and dealing with the package files, versions, etc? Leave a comment below!

Subscribe to my blog and get posts like this in your inbox. Share your email below, or follow me on Twitter and Threads.

npm install and the package-lock.json file (2024)
Top Articles
Russia: Instagram users 2024 | Statista
Accepting Payment by Manually Entering Credit Card Details | Help Center | Wix.com
Cpmc Mission Bernal Campus & Orthopedic Institute Photos
Rubratings Tampa
#ridwork guides | fountainpenguin
855-392-7812
Wisconsin Women's Volleyball Team Leaked Pictures
Crocodile Tears - Quest
Songkick Detroit
Wfin Local News
Decaying Brackenhide Blanket
Culver's Flavor Of The Day Monroe
Space Engineers Projector Orientation
Sams Gas Price Fairview Heights Il
How Many Cc's Is A 96 Cubic Inch Engine
OSRS Dryness Calculator - GEGCalculators
Best Suv In 2010
Crossword Nexus Solver
Echat Fr Review Pc Retailer In Qatar Prestige Pc Providers – Alpha Marine Group
Committees Of Correspondence | Encyclopedia.com
Star Wars: Héros de la Galaxie - le guide des meilleurs personnages en 2024 - Le Blog Allo Paradise
Golden Abyss - Chapter 5 - Lunar_Angel
Iroquois Amphitheater Louisville Ky Seating Chart
Amortization Calculator
Mybiglots Net Associates
Inbanithi Age
Airtable Concatenate
Jermiyah Pryear
Craigslist Wilkes Barre Pa Pets
Ltg Speech Copy Paste
Effingham Daily News Police Report
Tinyzonehd
Annapolis Md Craigslist
Jazz Total Detox Reviews 2022
Askhistorians Book List
Rush County Busted Newspaper
Franklin Villafuerte Osorio
Nsu Occupational Therapy Prerequisites
Wow Quest Encroaching Heat
Mississippi State baseball vs Virginia score, highlights: Bulldogs crumble in the ninth, season ends in NCAA regional
Can You Buy Pedialyte On Food Stamps
Geology - Grand Canyon National Park (U.S. National Park Service)
Top 25 E-Commerce Companies Using FedEx
Dee Dee Blanchard Crime Scene Photos
Mid America Clinical Labs Appointments
Aita For Announcing My Pregnancy At My Sil Wedding
Research Tome Neltharus
Best brow shaping and sculpting specialists near me in Toronto | Fresha
91 East Freeway Accident Today 2022
Minecraft Enchantment Calculator - calculattor.com
Varsity Competition Results 2022
Latest Posts
Article information

Author: Nathanael Baumbach

Last Updated:

Views: 6247

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Nathanael Baumbach

Birthday: 1998-12-02

Address: Apt. 829 751 Glover View, West Orlando, IN 22436

Phone: +901025288581

Job: Internal IT Coordinator

Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.