But what the hell is package-lock.json?
Tags: node, javascript, npm
Published: 2019-09-02 10:25:38

So yeah I am going to write about probably the most ignored file from our directories **package-lock.json** !!

package-lock.json is an extremely important file that is there to save you from a lot of **boom boom bam bam 🔥** in your repositories.

So before we get into package-lock.json let's talk about semantic versioning and package.json. 

# **1.** Semantic Versioning
Semantic versioning or SemVer is the ideal way of versioning packages. They are usually written like `1.4.5` (major.minor.patch)

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/uyw4yois1mkqr967ufb8.png)

## *1a.* Bug fix/patch version
Includes bug fixes/documentation spelling mistakes etc.

## *1b.* Minor version
Includes additions of functions or API which does not break anything from the older versions So anything that runs on v1.1.0 should work on v1.9.0 as well.

## *1c.* Major version
Includes version which breaks stuff. It can include removing APIs or changing names of functions so anything that works on v1.0.0 may not necessarily work on v2.0.0


# **2.** Package.json
package.json is a file that contains information about your project (name, version, etc) and it lists the packages that your project is dependent on.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/btvz35yyxvrj4tsbmpzl.png)

So as you can see in the picture above after every dependency listed under package.json there's a number something like `^2.20.0` which is the version of that package but before the version, there is `^`. So ^ this little guy can be a total destroyer for your project.

^ sign before the version tells npm that if someone clones the project and runs npm install in the directory then install the latest minor version of the package in his node_modules.

So lets say I am having express with `^2.20.0` in package.json and then express team releases version 2.24.0 and now when someone clone my repo and runs `npm install` in that directory they will get the version 2.24.0 (You can also put `~` instead of `^` it will update to latest patch version)

However, this can be a huge issue if package developers break any of the functions on the minor version as it can make your application break down.

So npm later released a new file called package-lock.json to avoid such scenarios

# **3.** package-lock.json

![package-lock.json meme](https://thepracticaldev.s3.amazonaws.com/i/do6l6okbr89h5pc666k0.png)
package-lock.json will simply avoid this general behavior of installing updated minor version so when someone clones your repo and run npm install in their machine. NPM will look into package-lock.json and install exact versions of the package as the owner has installed so it will ignore the `^` and `~` from package.json.

Also, it contains some other meta information which saves time of fetching that data from npm while you do npm install.


You can refer [npm blog](https://docs.npmjs.com/files/package-lock.json) for some more information on package-lock.json.

Thank You for reading this!

I hope this was useful 🎉 :)


EDIT: So while reading the comments I thought I should also explain how package-lock.json changes so here's one of the replies that I wrote that I think everyone should go through {% devcomment eoo4 %}

EDIT2 : Quoting from the comment of Kat Marchán
(https://dev.to/zkat/comment/epbj) She is the one who wrote `npm ci` and added package-lock.json to NPM

{% devcomment epbj %}