w3resource

Installing dependencies and working with version control


In modern JavaScript development, you can take advantage of libraries developed by other JavaScript developers. This will save you a lot of time during development and ensure that you don't reinvent the wheel, these libraries will be saved as dependencies, and yarn will update your package.json file which you have to commit to version control.

In this tutorial we will show you how to install dependencies as well as what the files in a version control should contain.

Installing dependencies

Whenever you check out you package from version control, you have to install its dependencies.

To install dependencies, you have to run yarn install in the root of your directory to install all the dependencies for a project. The dependencies will be retrieved from the package.json file you pushed to version control, and will be stored in the yarn.lock file.

Whenever you are developing a package, the installation of dependencies is mostly done after:

  • You just checked out code for a project and it needs these dependencies to function.
  • Another developer on the project has added a new dependency and you need to pick it up.

Installing Options

There are many ways to install dependencies, these include:

  • When you want to install all dependencies: yarn or yarn install.
  • Installing only a single version of a package: yarn install --flat
  • Forcing a re-download of all the packages: yarn install --force
  • Installing of production dependencies alone: yarn install ?production

Working with version control

To ensure an efficient use of your package, you will need to ensure that all the necessary files are checked into your source control system.

Required Files

There are certain files that must be included in your source control system, so that other developers can use your package, these include:

  • package.json: The package.json file has all the current dependencies for your package.
  • yarn.lock: The yarn.lock file will store the exact versions of each dependency for your package.
  • The actual source code that will provide the functionality for your package.

There are lots of version control systems that you can use, such as Gitlab, GitHub, BitBucket etc. They all have their pros and cons but in most cases they should be sufficient for your need, whether you need private storage or your need continuous integration.

Previous: The yarn workflow and creating a new project
Next: Managing dependencies and Upgrading dependency