What is package-lock.json? (2024)

This article is based on Node v16.15.1 and NPM 8.11.0.

This tutorial explains what the difference between package.json and package-lock.json is, and why package-lock.json can help to avoid installing modules with different versions.If you are not sure what the package.json is responsible for, check out this article - The basics of Package.json.

How package-lock.json manages the dependency tree

💰The Pragmatic Programmer: journey to mastery. 💰One of the best books in software development, sold over 200,000 times.

package-lock.json is a file generated by npm (since v5 2017), and it locks package dependencies and their sub-dependencies.It tracks only top-level dependencies, and their associated versions. Sounds simple right? Though each of these top-level dependencies can also have their own dependencies, and each of these can also have their own dependencies and so on.This relationship between all the dependencies and sub-dependencies in a project is called the dependency tree.The dependency tree represents every module our project depends on and what version is required.

Installing a dependency with npm actually fetches all the needed dependencies, and installs them into the node_modules/ folder.The package-lock.json file is a snapshot of our entire dependency tree and all the information npm needs to recreate the state of the node_modules/ folder.Also, when a package-lock.json file is present, npm install will install the exact versions specified.

The package-lock.json is not meant to be human-readable, and it's not meant to be edited manually.The npm CLI generates and manages it for us automatically.

Track package-lock.json

The package-lock.json file needs to be committed to version control (GIT) to make sure the same dependency tree is used every time.The benefit of committing the package-lock file to version control is tracking the state of the node_modules/ folderwithout having to commit the folder itself to version control. Never commit the node-modules folder.It is not intended to be committed, it's too big, and the state is already tracked.

Whenever we run a npm command that changes dependencies, like npm install <PACKAGE> or npm uninstall <PACKAGE> or npm update or any other command that alters dependencies,the package-lock.json file will be updated to reflect the state of the dependency tree.

npm-shrinkwrap

Locking dependencies is not a new concept in the Node.js ecosystem or in the programming world. The package-lock file behaves nearly like the already existing npm-shrinkwrap.json, which was how to lock a package before npm v5.The only difference is that the package-lock.json is ignored by npm when publishing to the NPM registry. If you want to lock your dependencies, when publishing a package you have to use npm-shrinkwrap.json.You should only have one of these files in your root directory. If both are present npm-shrinkwrap takes precedent.The recommended use-case for npm-shrinkwrap.json is applications deployed through the publishing process on the NPM registry.

To create a npm-shrinkwrap file, run npm shrinkwrap. This command renames your package-lock to npm-shrinkwrap. The files are functionally the same.npm-shrinkwrap should be used when publishing to the NPM registry.

TL;DR

  • package-lock.json is a snapshot of the entire dependency tree (all packages, all dependencies. all resolved version numbers)
  • It's a safeguard against dependency drifting between installs.
  • package-lock.json is updated automatically on dependency changes.
  • It should be committed to version control to ensure the same dependencies on install.

Thanks for reading and if you have any questions, use the comment function or send me a message @mariokandut.

If you want to know more about Node, have a look at these Node Tutorials.

References (and Big thanks):

NPM package-lock, NPM shrinkwrap, Node, HeyNode

What is package-lock.json? (2024)

FAQs

What is package-lock.json? ›

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.

Is it safe to delete the package lock json file? ›

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 difference between package lock json and package json? ›

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.

Do I need the package lock file? ›

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.

What is the role of package lock? ›

The goal of package-lock. json file is to keep track of the exact version of every package that is installed so that a product is 100% reproducible in the same way even if packages are updated by their maintainers. This solves a very specific problem that package.

Should I delete json files? ›

Delete the JSON files

This file is only useful if you have added any information like captions or descriptions to the photo within Google Photos. The information about where and when a photo was taken and any camera settings are stored in the photo file - they don't need the JSON file.

Why is package-lock json required? ›

package-lock. json is automatically generated for any operations where npm modifies either the node_modules tree, or package. json . It describes the exact tree that was generated, such that subsequent installs are able to generate identical trees, regardless of intermediate dependency updates.

What happens if I delete package-lock? ›

So when you delete package-lock. json, all those consistency goes out the window. Every node_module you depend on will be updated to the latest version it is theoretically compatible with. This means no major changes, but minors and patches.

What is the purpose of package json? ›

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.

What is the main purpose of the package-lock json file Linkedin? ›

The packge-lock. json file can accurately record the version number of the module used by the current project. If there is this file in the project, then NPM will install dependencies according to the version number recorded in package-lock.

Should you ever delete package-lock? ›

Conclusion: don't ever delete package-lock. json . Yes, for first level dependencies if we specify them without ranges (like "react": "16.12. 0" ) we get the same versions each time we run npm install .

Why use a lock file? ›

A lock file contains important information about installed packages and it should always be committed into your Package Manager source repositories. Not committing the lock file to your source control results in installing two different modules from the same dependency definition.

Do you commit both package json and package-lock json? ›

Its purpose is to track the entire tree of dependencies (including dependencies of dependencies) and the exact version of each dependency. You should commit package-lock. json to your code repository.

Can you 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 is package-lock so big? ›

The package-lock. json file lists your application's dependencies and the dependencies of all its dependencies. In other words, it describes which version of every single package you have installed. That's why it's so much longer than package.

How do you avoid package-lock conflicts? ›

How to resolve package-lock. json conflicts
  1. Update the master branch with the latest changes: git checkout master git pull.
  2. Merge your feature branch into master : git merge mybranch. ...
  3. Open your editor (e.g. VSCode) and: ...
  4. Install packages, which will re-generate package-lock.json : npm install.

What are the risks of JSON? ›

In some cases, JSON injection can lead to Cross-Site Scripting or Dynamic Code Evaluation. JSON has traditionally been parsed using an eval() function, but this is an insecure practice. Any code that uses eval() to deserialize the JSON into a JavaScript object is open to JSON injection attacks.

Are JSON files necessary? ›

The most common use of JSON data and files is to read data from a server for a website or web application to display — and change data given the correct permissions. But, that is not the only thing it is used for. Computer applications, programs, mobile apps, and much more all use JSON files.

Why are JSON files on my computer? ›

The main purpose of the JSON file was to transmit data between a server and a web application. But today, it serves many purposes. File Configurations: Many JavaScript applications like reactJS, node. js, and others that are server-based use this file to store configuration information.

How to disable package-lock in npm? ›

To tell npm not to create a package-lock. json lock file for your current project, create a file called . npmrc at the root of the project and add package-lock=false to it.

How do I fix vulnerability in json package-lock? ›

A dependency defined in ./package-lock. json has known security vulnerabilities and should be updated. The dependency is not defined in our package. json file.
...
The simplest/easiest way to fix this is:
  1. npm install <dep>
  2. npm uninstall <dep>
  3. npm update.
  4. npm install.
Mar 30, 2018

What is npm I package-lock only? ›

In regular situations the package-lock is meant to set a complete dependency tree of every package and its dependencies in your application, so every developer on a different machine will have the exact same tree.

How do I remove a package from json? ›

Uninstalling packages can be done by calling the npm uninstall command followed by the package name. Using this syntax in the command line will uninstall the package specified. Doing so will remove that package from the package. json folder of the current project.

Is it bad to delete yarn lock? ›

lock and generate it again by running yarn install? No need to delete the file, just run yarn and it'll update all dependencies.

Is it okay to remove yarn lock? ›

If it's an existing project you can just remove yarn. lock and continue using it with npm. I am using npm to manage packages for my project.

Should I commit package lock? ›

Yes, it is important to commit the package-lock. json file to your version control system. This ensures that all developers are using the same versions of the packages, which can help to avoid unexpected errors and bugs.

What creates a package json file? ›

To create a package. json file with values that you supply, use the npm init command. On the command line, navigate to the root directory of your package. Answer the questions in the command line questionnaire.

Do I need to create a package json file? ›

Creating a package. json file is typically the first step in a Node project, and you need one to install dependencies in npm. If you're starting a project from scratch, you create a package. json file to hold important metadata about your project and record your dependencies.

How to clear npm cache? ›

How to clear cache? To clear a cache in npm, we need to run the npm cache clean --force command in our terminal.

How to create a package lock json file? ›

The package-lock. json file is used to lock down the versions of your dependencies so that your project will always use the same versions, regardless of when you install it. To generate this file, you can use the "npm install" command with the "--save-exact" flag.

Should you update npm packages? ›

We recommend regularly updating the local packages your project depends on to improve your code as improvements to its dependencies are made. To test the update, run the outdated command. There should not be any output.

Can I delete lock files? ›

Open the Start menu, then the File Explorer and find the formerly locked file. Remove the file. Click the previously locked file, then the “Home” tab, and “Delete” in the toolbar. Or, you may select the file by clicking on it and then pressing the “Delete” key.

Which is better yarn or npm? ›

Is Yarn better than NPM? In terms of speed and performance Yarn is better than NPM because it performs parallel installation. Yarn is still more secure than NPM. However, Yarn uses more disk space than NPM.

How to merge two json data? ›

simple. JSONObject to merge two JSON objects in Java. We can merge two JSON objects using the putAll() method (inherited from interface java.

Is package-lock json needed for deployment? ›

json file is needed for deployment in most cases. Because when you deploy your application, the package manager either npm or yarn will use the information in the package-lock. json file to install the exact same versions of the packages and dependencies that you have been working with during development.

Is package json created automatically? ›

The package. json is an auto-generated Node. js NPM package file for your project. You cannot directly edit this file from within Autocode.

Why are some npm packages listed in lock file but not the package json file? ›

It's not listed in your package. json because it is a nested dependency. You can update it either by trying npm audit --fix or you use the package npm-force-resolutions.

Should I use npm install or npm CI? ›

npm ci just installs existing dependencies, in contrast to npm install, which attempts to update current dependencies if possible. This ensures that the builds in continuous integration are reliable. It's better to use npm i in development and npm ci for production.

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

Package. JSON holds all of the “npm” packages installed for the project. Angular. JSON holds the configuration for the project.

How is package-lock created? ›

package-lock.json is automatically generated for any operations where npm modifies either the node_modules tree, or package.json . It describes the exact tree that was generated, such that subsequent installs are able to generate identical trees, regardless of intermediate dependency updates.

How to update only one package in package-lock? ›

Run npm update <package> . This will update it to the latest version that satisfies the requirements specified in your package. json and reflect the update in the package-lock.

How does package-lock get updated? ›

package-lock. json file is essentially used to lock dependencies to a specific version number. This file is automatically generated (or re-generated) when there is a change in either the node_modules tree or package. json file.

Is it okay to delete Yarn lock file? ›

lock and generate it again by running yarn install? No need to delete the file, just run yarn and it'll update all dependencies.

Is package json required? ›

If you're not publishing your project to the NPM registry or otherwise making it publicly available to others, your package. json is still essential to the development flow. Your project also must include a package. json before any packages can be installed from NPM.

Is it safe to delete json files on Mac? ›

Yes, you can. You delete an alias the same way you delete any file on the Mac: move it to the Trash.

Should I ignore yarn lock? ›

Every project using yarn should commit the yarn lockfile to source control. The lockfile is the source of truth for telling other developers how to install dependencies for your project. Without this lockfile, other developers will be at risk for installing the wrong packages.

Do I need package lock with yarn? ›

Without a package lock file, a package manager such as Yarn or npm will resolve the the most current version of a package in real-time during the dependencies install of a package, rather than the version that was originally intended for the specific package.

Why do we need lock files? ›

A lock file contains important information about installed packages and it should always be committed into your Package Manager source repositories. Not committing the lock file to your source control results in installing two different modules from the same dependency definition.

What is package json and what is it used for? ›

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.

Why do we create package json? ›

Creating a package. json file is typically the first step in a Node project, and you need one to install dependencies in npm. If you're starting a project from scratch, you create a package. json file to hold important metadata about your project and record your dependencies.

Are json files important? ›

The most common use of JSON data and files is to read data from a server for a website or web application to display — and change data given the correct permissions. But, that is not the only thing it is used for. Computer applications, programs, mobile apps, and much more all use JSON files.

What to files should I not delete on my Mac? ›

Don't delete or move files within System and Library folders appearing when you first double click your hard disk. Don't delete or move . App files in your Applications folder, these are Mac OS X native applications.

What can I safely delete from my library folder Mac? ›

We'll look at some folders you can delete on your Mac without causing any damage.
  1. Attachments in Apple Mail Folders. ...
  2. Past iTunes Backups and Large Files. ...
  3. Your Old iPhoto Library. ...
  4. Leftovers of Uninstalled Apps. ...
  5. Unneeded Printer and Scanner Drivers. ...
  6. Cache and Log Files.

How do I remove unwanted JSON data? ›

To remove JSON element, use the delete keyword in JavaScript.

How to resolve issues with package-lock json? ›

How to resolve package-lock. json conflicts
  1. Update the master branch with the latest changes: git checkout master git pull.
  2. Merge your feature branch into master : git merge mybranch. ...
  3. Open your editor (e.g. VSCode) and: ...
  4. Install packages, which will re-generate package-lock.json : npm install.

Top Articles
Latest Posts
Article information

Author: Laurine Ryan

Last Updated:

Views: 6072

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Laurine Ryan

Birthday: 1994-12-23

Address: Suite 751 871 Lissette Throughway, West Kittie, NH 41603

Phone: +2366831109631

Job: Sales Producer

Hobby: Creative writing, Motor sports, Do it yourself, Skateboarding, Coffee roasting, Calligraphy, Stand-up comedy

Introduction: My name is Laurine Ryan, I am a adorable, fair, graceful, spotless, gorgeous, homely, cooperative person who loves writing and wants to share my knowledge and understanding with you.