Skip to main content

Yarn

At Figuro, we use Yarn 2 as our package manager for JavaScript and TypeScript projects. Yarn 2 offers many advantages over previous versions of Yarn, including faster performance, better caching, and improved stability. One of the key features of Yarn 2 is its ability to manage project dependencies through a combination of a lockfile and an advanced resolution algorithm.

Enabling Yarn 2

To enable Yarn 2 in your project, first make sure you have it installed globally:

npm install -g yarn

Then, create a new project directory and navigate into it:

mkdir my-project
cd my-project

Initialize a new project with Yarn 2:

yarn set version berry
yarn init

This will create a new package.json file and a .yarnrc.yml file with Yarn 2 configuration options.

Using Yarn 2

To install dependencies, simply use the yarn command followed by the package name:

yarn add react

To run a script defined in your package.json file, use the yarn run command followed by the script name:

yarn run start

To upgrade a package to its latest version, use the yarn upgrade command followed by the package name:

yarn upgrade react

Yarn 2 also offers many other features, such as workspaces, which allow you to manage multiple related packages in a single repository, and plugins, which can extend Yarn's functionality. Check out the official documentation for more information on how to use Yarn 2 in your projects.

Workspaces

Yarn 2 workspaces are a powerful feature that allow developers to manage multiple packages in a single repository, while still providing each package with its own node_modules folder and independent dependency tree. Workspaces simplify the management of interdependent packages and make it easier to develop and test changes across the entire project.

To use workspaces, simply add a workspaces field to the package.json file in the root of your project, listing the directories of the packages you want to include. For example:

{
"name": "my-monorepo",
"private": true,
"workspaces": ["packages/*"]
}

This configuration tells Yarn 2 to include all directories inside the packages directory as workspaces. Then, each package can be defined as a separate package in its own package.json file.

Once you have defined your workspaces, you can install dependencies for all of them with a single command:

yarn install

This will create a shared .yarn folder in the root of your project that contains all of the dependencies for your workspaces. From there, you can manage your packages like any other package with Yarn 2, including adding, removing, and upgrading dependencies.

Overall, workspaces are a powerful tool for managing large projects with multiple interdependent packages. By using Yarn 2 workspaces, you can streamline your development process, simplify dependency management, and make it easier to maintain and test your entire project as a whole.

npm disclaimer

danger

Figuro discourages the use of npm as a package manager for JavaScript projects. While npm has been a popular choice for many years, it has some limitations and drawbacks that have led us to explore other options such as Yarn 2. One of the main issues with npm is its inability to manage dependencies effectively, which can lead to version conflicts and other problems. Additionally, npm lacks some of the features and optimizations that are available in Yarn, such as automatic workspace installation and caching of package downloads. For these reasons, we highly recommend using Yarn 2 for JavaScript projects at Figuro.

Resources