Configure NPM Workspaces in your Package.json

Share this video with your friends

Send Tweet
Published 2 years ago
Updated 2 years ago

For this course, we are going to be using NPM Workspaces to configure our monorepo. We will have two workspaces, one for our apps and one for our libraries. We configure those in our package.json.

Alejandro Ñáñez: [0:00] Let's start by creating our package.json file by running npm init -y. [0:05] Now let's have the workspaces key, which is the one that will tell NPM to behave like a monorepo. Here, in this workspaces array, we are going to add all the folders that we want to be added to our monorepo. For this course, we are going to use apps and everything that is inside of it, and we are also going to use libs. Let's clean up things a little bit because we do not need test for now.

[0:32] Now, let's update the name property to monorepo. This could be anything, but the convention that we would like to use is the @ symbol and then the name of your company or your own name and all this is because in the future, when we have many packages in our monorepo, we will start calling them @monorepo/mypackage1, 2 and so forth.

[0:59] To recap, in order to have a monorepo managed by NPM Workspaces, we will need to add the workspaces property with an array of strings. Each one of the strings in the array represent the folder that will contain applications, libraries and whatever we want. This could also be just one folder, but for organization purposes, I prefer to use apps and libs.

[1:22] It is not mandatory, but it's a good practice to update the name of your package.json file with something that it starts with @, and then followed by your company name or your own name. We will have a standard way to name your packages in applications inside the monorepo.

~ 2 years ago

I was using the npm v6 and node v14 and it's not worked for me until I upgrade my node and npm version. Please mention this at the start of the course otherwise some users might not be able to build monorepo in their machine.