Run App Specific Commands in a Monorepo

Share this video with your friends

Send Tweet
Published 2 years ago
Updated 2 years ago

To run specific commands inside a monorepo we have to type npm run then the name of the command, build. We then add the flag -w for watch mode and specify which package we want to run the build command. In this case, it’s going to be the utils package.

The full command will look like, npm run build -2 @monorepo/utils.

If you want to run all of the commands, you'll need to add the -ws flag.

The full command for running all of our build commands will look like, npm run build -ws.

Alejandro Ñáñez: [0:00] To run in a specific command inside a package of a monorepo, we have to type npm run, then the name of the command, and then we have to add the -w flag. Here, we have to specify in which package we want to run the build command. In this case, it's going to be in utils.

[0:20] Here, you can see that we are running the build script inside a utils package of the monorepo. If what we want is to run the build command across all the packages in the monorepo, we will have to add the -ws flag.

[0:36] Now that the command has ended, we can see that we are running build in the block project first, then we are running build in the dashboard project, and then we are running build inside the utils project.

[0:49] To recap. Running commands inside an npm workspaces monorepo is easy. We just have to type npm run, then the name of the command, and then we can add the -w flag to specify where we want to run that command, or we can add the -ws flag to run that command across all the packages in the monorepo.

Rob
Rob
~ a year ago

Hi Alejandro, first up, great course, really enjoying it

For me, running node v18.13.0 and npm 8.19.3 (Apr 2023) to get this working I had to run the following command ...

npm init -w name-of-workspace

I then went thru the interactive prompts to setup up the package.json file.

Then once that was done the npm run command would work as you describe here.

Running the npm init -w command seemed to respect and retain exiting content of pre-existing package.json files but of course was especially useful when there we no existing package.json.

Apologies, if you mention this elsewhere (I am racing thru the course and coding as I go!)

Thank you again for a great course! Feel free to reach out to me if you need any clarification on this feedback. Rob Kielty

Alejandro Ñáñez
Alejandro Ñáñezinstructor
~ a year ago

Hey Rob, thanks for reaching out!

Do you happen to have a repo that I can take a look at? I’m happy to help!

And I’m glad you like the course so far! ❤️