147

Is there a way to specify a working directory for yarn? This would be different then the --modules-folder option. Specifically, I'm trying to run the yarn install command from a location outside of the package location.

Similar to -C in git

mrbnetworks
  • 2,547
  • 3
  • 17
  • 10

2 Answers2

246

You can use --cwd like so yarn --cwd <path> <yarn command>.
The order of arguments is important.

Example:

yarn --cwd ~/test_project/ dev

Because the following will not work:

yarn dev --cwd ~/test_project/
madav
  • 2,918
  • 1
  • 13
  • 17
  • 28
    The equivalent for `npm` is `npm --prefix` – Hanxue Jan 13 '19 at 14:32
  • What does tilde mean? absolute path to root? – adi518 Mar 07 '20 at 15:05
  • 4
    Oh, that's the Linux notation. It doesn't work for Windows obviously. Had to use the regular `cwd` notation (`./`) instead. – adi518 Mar 08 '20 at 16:06
  • 2
    ~ is the absolute path to the current user, not specifically to root. – Fredrik Jonsén Apr 17 '20 at 09:09
  • 1
    @adi518 It's not strictly true to call ~/whatever "Linux" notation; ~ is expanded by the shell you're using (almost always to the home directory of the current user - I don't know of shells that do something else, but they may exist), not by the operating system. Linux has a much larger variety of shells than Windows does, too. This tends to bite people who don't realize that the shell is the thing that understands ~, when they use it in non-shell environments. – James Moore Dec 03 '20 at 00:15
  • How do I run the same on windows if the folder I want to run it is `client` in the main folder and I'm running this from `server` folder? @madav – Akhila Dec 28 '20 at 10:54
  • @Akhila Maybe try using a full path to client instead of using the ~ for root. – madav Dec 28 '20 at 19:48
  • @Akhila It's impossible to answer your question without knowing the relative location of the `client` and `server` folders. – JLRishe Mar 12 '21 at 08:23
151

--cwd is what you want.

(tested with yarn 1.3.2)

Tim Green
  • 3,571
  • 2
  • 23
  • 23
  • 1
    Official docs for this cli option: https://classic.yarnpkg.com/en/docs/cli/#toc-cwd – dpix Mar 29 '21 at 21:50