vdegenne
vdegenne

Reputation: 13270

Why does yarn install dev dependencies but I just need the builds?

If I use yarn add <package-name>, Yarn will install both dependencies and devDependencies of <package-name>. Is it intended?

I checked the documentation but I couldn't find a way to prevent installing the development dependencies. devDependencies are the dependencies that were used to compile the sources of one package, therefore if I am in a production environment I don't need them.

Upvotes: 83

Views: 115954

Answers (6)

kimy82
kimy82

Reputation: 4475

Since yarn 3 and more recents version (4 at least)

--production flag doesn't exist anymore. Use this command instead:

yarn workspaces focus --production

More details here https://yarnpkg.com/cli/workspaces/focus

Old answer yarn 1 & 2 compatible

Use --production=true (or simply --production or --prod for short). It is indeed normal behaviour; Yarn assumes you are in a 'development' context unless your NODE_ENV environment variable is set to 'production'.

Have a look at Yarn's documentation.

Upvotes: 107

robert
robert

Reputation: 31

With Yarn 2+, it seems that Yarn focuses on PnP installs, meaning a content-addressable data-structure that replaces classic node_modules and is assumed to be part of the package, hence package sources will automatically include devDependencies.

Nevertheless, yarn workspaces focus <name> --production allows to install a workspace package without `devDependencies. See https://yarnpkg.com/cli/workspaces/focus for more information.

Upvotes: 3

contrebis
contrebis

Reputation: 1318

The existing answers seem to miss the point of the question (including the accepted answer).

If I invoke yarn add <my-package>, Yarn will install both dependencies and devDependencies of <my-package>. Is it normal behavior?

No, this sounds like a bug. I don't see this behaviour with the latest version of yarn v1.

If I yarn add foo to my project, I should then have installed:

  • my direct dependencies
  • foo
  • any dependencies of foo and their dependencies recursively
  • not devDependencies of foo
  • devDependencies of my project (unless using various production flags/vars mentioned in other answers)
  • (as far as I know, yarn doesn't automatically install peer dependencies as in recent npm versions)

If you are running yarn add it can be assumed you are in a "dev" context.

Upvotes: 2

zyfyy
zyfyy

Reputation: 363

NODE_ENV=production also prevent install devDependencies

Upvotes: 8

serv-inc
serv-inc

Reputation: 38207

As said in the comment by @ddotsenko

Not "broken" but "badly designed" --prod still downloads and "installs" dev packages IF yarn needs to resolve "full tree". Just use yarn install --production --frozen-lockfile and matching yarn.lock and --production will work as expected.

That worked to remove a 210 MB node_modules to 70 MB, similar to npm and pnpm.

Upvotes: 16

Webvoid
Webvoid

Reputation: 511

Yarn has a --production option, which will cause it to install only production dependencies. This is shown here

Upvotes: 1

Related Questions