Skip to content

/flags/yarn/yarn-cwd

yarn --cwd DIR: run Yarn in another directory without cd

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
Yarn
command
yarn
flag
--cwd DIR
default
the current directory

yarn --cwd client build runs yarn build as if you had first changed into client/: Yarn reads client/package.json, installs into client/node_modules and runs the script there. It is handy in a root package.json that drives a frontend and a backend kept in subfolders, and in CI steps where changing directories is awkward. The option belongs to Yarn 1 (Classic); in Yarn 2 and later use yarn workspace NAME ... or run the command from the folder.

Example

bash
mkdir client
cat > client/package.json <<'EOF'
{ "name": "client", "version": "1.0.0", "license": "MIT", "scripts": { "where": "node -p \"require('./package.json').name\"" } }
EOF
cat > package.json <<'EOF'
{ "name": "root", "version": "1.0.0", "license": "MIT", "private": true }
EOF
echo "--- yarn --cwd client run where"
npx --yes yarn@1 --cwd client -s run where
echo "--- without --cwd (root has no such script)"
npx --yes yarn@1 -s run where 2>&1 | head -1

Output

--- yarn --cwd client run where
client
--- without --cwd (root has no such script)
error Command "where" not found.

The script prints the name of the package it ran in. Without --cwd, the root has no where script and Yarn 1 answers with the error explained in error Command "where" not found.

Typical uses

CommandEffect
yarn --cwd client installInstall the dependencies of client/
yarn --cwd client add reactAdd a dependency to client/package.json
yarn --cwd server startRun the start script of server/
"build": "yarn --cwd client build && yarn --cwd server build"Root script that builds both parts
yarn workspace client buildWorkspace alternative (Yarn 1 workspaces and Yarn 2+)
npm --prefix client run buildnpm equivalent

--cwd versus workspaces

--cwd treats each folder as an independent project: two node_modules folders, two lockfiles, no sharing. Workspaces ("workspaces": ["client", "server"] in the root package.json) install everything once, link local packages to each other and keep one yarn.lock. For a real monorepo, workspaces are the better structure and yarn workspace NAME cmd replaces --cwd.

In Yarn 2 and later, running a command inside a folder that is not declared as a workspace of the parent project fails with The nearest package directory doesn't seem to be part of the project; declare the workspace or give the folder its own empty yarn.lock.

Pitfalls

  • The path is relative to the directory you run Yarn from, not to the root package.json.
  • Put --cwd before the command; after it, the option is passed to the script.
  • --cwd does not imply --frozen-lockfile or any other option of the parent call; repeat them for the child directory.
  • Mixing Yarn versions: if client/ pins another Yarn through packageManager, Corepack may run a different Yarn there.

FAQ

What is the difference between yarn --cwd and yarn workspace?

--cwd changes the working directory, so it works for any folder with a package.json. yarn workspace NAME selects a workspace by package name and only works inside a workspaces project; it is the documented way in Yarn 2 and later.

Does --cwd have to come first?

Yes. It is a global option: yarn --cwd client build. Written after the command, as in yarn build --cwd client, it is passed to the script as an argument instead.

How do I do the same with npm?

npm uses --prefix: npm --prefix client run build. Note that npm install --prefix also changes where packages are installed.

Sources