Install Dependencies for Specific Packages in a Monorepo

Share this video with your friends

Send Tweet
Published 2 years ago
Updated 2 years ago

We can install a dependency inside a monorepo by running npm install, then the name of the dependency, in our case, it’s going to be Lodash. But, we are going to add different flags to that.

If we use the -w flag, we would have to specify in what package we want to install Lodash. We will do this for monorepo/utils. We could also use the -ws flag to install Lodash across all the packages in the monorepo. We can also use -W in uppercase to install Lodash in the root of the project.

Instructor: [0:00] We would install an npm package inside a package of a monorepo by running npm install, then the name of the package. Then we add the -w flag. We specify where we want it. In this case, we want to install Lodash inside the utils package. Let's check out the package.json file inside the utils folder. There you have it. We have Lodash.

[0:23] If we run git status, we'll see that we have modified the package.json file inside the utils package. We could install Lodash across all the packages in the monorepo by adding the -ws flag.

[0:38] Now, let's run git status to see what has changed. Yes, the app's package-lock.json file has changed. Same for the dashboard and same for the utils.

[0:48] We could also install one dependency in the root of the monorepo by running npm install, the name of the dependency, then we use the -W, this time with uppercase. Let's run git status to see what has changed. This time, we modified the package.json file that is in the root of the project.

[1:08] To recap, we can install a dependency inside a monorepo by running npm install, then the name of the dependency, in this case is going to be Lodash. We are going to add different flags.

[1:20] If we use the -w flag, we would have to specify in what package we want to install Lodash. In this case, it could be monorepo/utils. We could also use the -ws flag to install Lodash across all the packages in the monorepo. We can also use -W in uppercase to install Lodash in the root of the project.

Yujing Zheng
Yujing Zheng
~ 2 years ago

What's the use case for installing libraries in the root package.json?

Alejandro Ñáñez
Alejandro Ñáñezinstructor
~ 2 years ago

@Yujing great question.

I’d recommend installing as few dependencies as possible in the root package.json file. The ones that I can think of right now are, concurrently, Turborepo and Husky. The reason for those libraries to exist in your monorepo is to help you “manage” your project; for instance, you’d want to use concurrently to run all your scripts or Husky to create git-hooks that affect all your projects.

My default approach is to install all the dependencies you need for a project inside the project itself; for example, you’ll want the blog and the dashboard projects to have their version of React installed; if you do that, you can easily update one project independently without much trouble. Another benefit of this approach is that you know exactly what dependencies Your project uses.

Alejandro Ñáñez
Alejandro Ñáñezinstructor
~ 2 years ago

@Yujing, feel free to ask again if you still have questions! 👍

~ 8 months ago

I can't find any of these commands or flags under npm docs. Where do you learn this stuff?