divert yarn add to different path without --cwd

Viewed 1083

I have a project with root folder and client folder, both having package.json files.

On the root folder I don't want to install any packages, ever. Instead, I want that running $ yarn add some-package will automatically add it in the client folder, as if I would do $ yarn add some-package --cwd ./client

I tried doing cd client and yarn --cwd ./client in scripts.preinstall, but it didn't work, which I think it's because preinstall and install are different processes.

I also tried running a cusotm bash script on preinstall but didn't find a way to pass the args from the command line to it, so they get "swollen" by yarn.

Any ideas how to accomplish this?

I'm fairly new with bash, but I can research the solution with some guidance.

Thanks in advance, make it a great day!

PS. I can hack it by hijacking the yarn command on my shell, and check if I'm ot the aforementioned project folder and if the command was yarn add, but I prefer to have a solution for all our developers, and not to pollute my shell.

1 Answers

The solution is to use a .yarnrc file in your root project folder next to your package.json.

Add this line to .yarnrc:

# install modules in the specified location
--modules-folder ./client

Note: If you already have a node_modules folder feel free to delete it after creating .yarnrc and follow it by running yarn install for all your packages to be downloaded again into your target directory.

Related