77

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.

6 Answers 6

95

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.

7
  • 5
    This functionality seems to be broken due to github.com/yarnpkg/yarn/issues/6323
    – brodybits
    Jun 18, 2019 at 1:54
  • 9
    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.
    – ddotsenko
    Jul 5, 2020 at 20:54
  • 7
    --production flag has been deprecated for yarn 2.0. How to prevent yarn 2.0 from installing dev dependencies?
    – akshat
    Apr 2, 2021 at 5:45
  • 6
    Late to the party but the new way to do this in Yarn 2+ is by using yarn workspaces focus --production --all: yarnpkg.com/cli/workspaces/focus
    – tomdaly
    Dec 8, 2021 at 14:41
  • 5
    Unfortunately I don't have an answer, but I'd like to note that none of the above is correct as of January 2022. Yarn will always install devDependencies regardless of options and NODE_ENV envvar.
    – Resisty
    Jan 26, 2022 at 1:07
14

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.

5

NODE_ENV=production also prevent install devDependencies

1
  • 3
    use NODE_ENV=production as environments before excute yarn or yarn install
    – zyfyy
    Dec 7, 2021 at 4:26
2

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.

1
  • Please share your package.json, and share more information about your environment such as the package manager that you used yarn|npm|pnpm and what the versions and system information. Sep 15, 2022 at 15:06
1

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.

0

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

1
  • 3
    bro this flag doesn't even work... that's the main issue Aug 8, 2022 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.