Why you should use package-lock.json - LogRocket Blog (2024)

In this article, we’ll look at package-lock.json, why it’s important, and how it’s best used along with NPM CLI in your day-to-day life.

History

NPM version 5 introduced package-lock.json as a mechanism to capture the exact dependency tree installed at any point in time.

This helps with collaboration across different environments in which you want everyone fetching dependencies for a specific version of your project to fetch the same tree.

package.json defines the required dependencies and their respective versions using semantic versioning. However, semantic versioning can be tricky.

Consider a dependency stated as "express": "^4.16.4".

The publisher of this module (without using package-lock.json) would have express version 4.16.4 installed since they installed the latest version.

If express has published a new version by the time I download this module and try to install dependencies on it, I can download the latest version.

The caret symbol tells us exactly that.

The problem with the above is that if version 4.17.x contains a bug, my local setup will fail, but the publisher’s will continue to work fine on the previous version.

The same thing could happen in the production environment, and you’d have no idea why it was failing.

Prior to NPM version 5, you would use shrinkwrap. It differs from package-lock.json because it’s allowed to be published with your module on the NPM registry, whereas package-lock.json is not.

If all members can use NPM+5, it’s best to go with package-lock.json for unpublished projects.

But if you are developing a module and you intend to publish it, you might need to think about whether you want the clients to install the exact dependency tree you dictate, or if you want to be more flexible about it. Here’s a more detailed version on the subject.

So, package-lock.json will describe the exact dependency tree currently installed. The format is described in the NPM documentation page.

By committing it to your VCS — which you should absolutely do — you can go back in history and replicate the exact dependency tree from that time.

Make sure to always commit package-lock.json to your VCS to keep track of exact dependency trees at any given time.

It will ensure that all clients that download your project and attempt to install dependencies will get the exact same dependency tree. Furthermore, it’ll ensure you’re able to check out previous commits and replicate the dependencies state of each commit.

package.json vs package-lock.json

Make sure you don’t change package-lock.json directly. That’s being handled automatically by NPM. It reflects changes made to package.json to package-lock.json and keeps it up to date.

However, this only happens if you use NPMs’ CLI to make changes. If you manually change package.json, don’t expect package-lock.json to update. Always use the CLI commands, like install, uninstall, etc.

How to use the NPM CLI

NPM will auto-generate a package-lock.json when you first use it in a fresh project.

Then, you can use NPM as normal.

npm install (with specific modules as arguments)

install can be used with the names of modules to install as arguments, which will alter both package.json and package-lock.json since the dependency tree will change.

More great articles from LogRocket:

Consider the following example:

npm install express body-parser cors

npm install (without arguments)

install will attempt to install all dependencies in respect to package-lock.json.

A key point here is that install can alter package-lock.json if it registers that it’s outdated.

For example, if someone manually alters package.json — say, for example, they remove a package since it’s just a matter of removing a single line — the next time that someone runs npm install, it will alter package-lock.json to reflect the removal of the previous package.

That can be tricky. Imagine pulling the latest version of your project, running npm install to get up to date, only to find that you immediately have a bunch of changes in your tree that make no sense.

It’s also highly likely that the changes in your tree would make no sense to the people reviewing your changes.

npm uninstall

Similar to install but with names of modules to remove as arguments. This will alter both package.json and package-lock.json.

npm update

update will read package.json to find any dependencies that can be updated. Subsequently, it will construct a new dependency tree and update the package-lock.json as well.

Remember semantic versioning? Say we have a dependency in our package.json stated as ^1.4.5.

The ^ character tells NPM to check if there’s a newer version under the 1.X.X scope and if there is, to install that. Similarly, the ~ character will only go up to hot-fixes, or 1.4.X.

You could also omit the special character and keep a fixed version, which makes package-lock.json less helpful (but not useless).

npm ci

ci will install all dependencies in respect to package-lock.json similar to install. The key difference here is that it won’t alter package-lock.json under any circ*mstances.

Its purpose is to be used by environments, e.g. build servers, where installation happens in an automated way.

Conclusion

Remember these key takeaways when using package-lock.json:

Don’t use npm install without arguments to fetch dependencies — use npm ci for that. You can use the npm install to install specific dependencies.

Use npm ci everywhere when you only want the local dependencies tree — even on your local development environment.

Make a repetitive task, say once a month, to update your dependencies. (Alternatively, you can use a service like dependabot, but make sure that you have a good test coverage).

This way, you can ensure that your dependencies are up to date and avoid bubbling up technical debt.

More references

  • Differences between npm install and npm ci: https://stackoverflow.com/questions/52499617/what-is-the-difference-between-npm-install-and-npm-ci
  • NPM CLI source code: https://github.com/npm/cli/blob/latest/lib/install.js
  • Semantic versioning: https://blog.npmjs.org/post/162134793605/why-use-semver

plug

  1. Visit https://logrocket.com/signup/ to get an app ID
  2. Install LogRocket via npm or script tag. LogRocket.init() must be called client-side, not server-side
    • npm
    • Script tag
    $ npm i --save logrocket 

    // Code:

    import LogRocket from 'logrocket';
    LogRocket.init('app/id');

    Add to your HTML:

    <script src="https://cdn.lr-ingest.com/LogRocket.min.js"></script>
    <script>window.LogRocket && window.LogRocket.init('app/id');</script>

  3. (Optional) Install plugins for deeper integrations with your stack:
    • Redux middleware
    • NgRx middleware
    • Vuex plugin

Get started now

Why you should use package-lock.json - LogRocket Blog (2024)

FAQs

Why you should use package-lock.json - LogRocket Blog? ›

The purpose of package-lock. json is to ensure that the same dependencies are installed consistently across different environments, such as development and production environments. It also helps to prevent issues with installing different package versions, which can lead to conflicts and errors.

Should you use package lock? ›

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.

Why is the package json file important? ›

The package. json file contains descriptive and functional metadata about a project, such as a name, version, and dependencies. The file provides the npm package manager with various information to help identify the project and handle dependencies.

Does pnpm replace npm? ›

pnpm stands for performant npm. Which is a package manager for NodeJS based projects. which focuses on speed, and an efficient way of handling disk space. It is an alternative to npm and yarn .

Is package lock json needed for npm install? ›

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.

Why is package-lock so big? ›

The package-lock. json file is used to keep track of every installed package version so that probably explains why it added alot of lines.

Is it OK to remove package-lock? ›

Think about it, if you delete package-lock and re-install, you are forcing the latest versions of all packages in the dependency tree. Meaning, you are changing the behavior of (potentially) the entire application.

What is the point of package-lock JSON? ›

It records the exact version of every installed dependency, including its sub-dependencies and their versions. The purpose of package-lock. json is to ensure that the same dependencies are installed consistently across different environments, such as development and production environments.

What is the difference between JSON and package JSON? ›

json is a lockfile that holds information on the dependencies or packages installed for a node. js project, including their exact version numbers. The package for your project. json is the primary format for configuring and describing how to communicate with and execute your application.

What is the benefit of JSON schema? ›

Advantages of JSON schema
  • A properly defined JSON schema makes the JSON document intelligible for humans and computers.
  • It provides documentation for JSON documents.
  • It provides an easy way of validating JSON objects in an application, enabling interoperability across programming languages by maintaining consistency.
Feb 16, 2023

Why do people still use npm? ›

npm allows developers to easily find and install packages, manage their dependencies, and share their packages with others. With npm, developers can easily add functionality to their projects by installing packages and easily manage multiple versions of packages and their dependencies.

Why npm is better than pnpm? ›

npm maintains a flattened dependency tree as of version 3. This leads to less disk space bloat, with a messy node_modules directory as a side effect. On the other hand, pnpm manages node_modules by using hard linking and symbolic linking to a global on-disk content-addressable store.

Should I use pnpm in production? ›

Faster installs: Since pnpm avoids duplicating dependencies on disk, it can install packages much faster than npm or Yarn. In some cases, pnpm can be up to 10 times faster than npm! 2. Smaller disk usage: By avoiding duplication, pnpm can save a significant amount of disk space.

Do we need both package json and package-lock json? ›

To avoid differences in installed dependencies on different environments and to generate the same results on every environment we should use the package-lock. json file to install dependencies. Ideally, this file should be on your source control with the package.

Is it OK to manually edit package-lock json? ›

If you are manually editing files in node_modules , it is generally best to delete the file at node_modules/.package-lock.json . As the hidden lockfile is ignored by older npm versions, it does not contain the backwards compatibility affordances present in "normal" lockfiles.

How to resolve conflicts in package-lock json? ›

json file and run npm install again - similarly with Yarn.
  1. rm package-lock.json npm install git add package-lock.json.
  2. git checkout --theirs package-lock.json.
  3. git config --global merge.theirs.name "Keep upstream changes" git config --global merge.theirs.driver "cp -f '%B' '%A'"
  4. npx npm-merge-driver install --global.
Sep 20, 2022

What does package-lock only do? ›

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. The package for your project.

What does no package-lock do? ›

The --no-package-lock argument will prevent npm from creating a package-lock. json file. This makes it seem like a suitable flag to run in CI, however we've found that --no-package-lock actually causes npm to also not use a present package-lock. json .

What does -- no package-lock do? ›

Using --no-package-lock skips the package-lock. It is neither read nor written as if the package-lock feature did not exist. So the package-lock. json file isn't used at all when the --no-package-lock is on.

Should I use a package manager? ›

In theory, you may not need a package manager and you could manually download and store your project dependencies, but a package manager will seamlessly handle installing and uninstalling packages. If you didn't use one, you'd have to manually handle: Finding all the correct package JavaScript files.

Top Articles
Latest Posts
Article information

Author: Neely Ledner

Last Updated:

Views: 6210

Rating: 4.1 / 5 (42 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Neely Ledner

Birthday: 1998-06-09

Address: 443 Barrows Terrace, New Jodyberg, CO 57462-5329

Phone: +2433516856029

Job: Central Legal Facilitator

Hobby: Backpacking, Jogging, Magic, Driving, Macrame, Embroidery, Foraging

Introduction: My name is Neely Ledner, I am a bright, determined, beautiful, adventurous, adventurous, spotless, calm person who loves writing and wants to share my knowledge and understanding with you.