Why will yarn install dev dependencies when I just need the builds?

47,531

Solution 1

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.

Solution 2

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.

Solution 3

NODE_ENV=production also prevent install devDependencies

Solution 4

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

Share:
47,531

Related videos on Youtube

vdegenne
Author by

vdegenne

github instagram

Updated on July 09, 2022

Comments

  • vdegenne
    vdegenne almost 2 years

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

    I checked the documentation but I couldn't find a way to prevent it from installing the development dependencies (which I don't need). I believe devDependencies are the dependencies that were used to compile the sources into the build scripts. And building my app I just need the builds.

  • brodybits
    brodybits almost 5 years
    This functionality seems to be broken due to github.com/yarnpkg/yarn/issues/6323
  • ddotsenko
    ddotsenko almost 4 years
    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.
  • serv-inc
    serv-inc over 3 years
    @ddotsenko : that is so helpful, it is now an answer . Please write your own for your well-deserved credit and @ notify me for mine to be deleted.
  • akshat
    akshat about 3 years
    --production flag has been deprecated for yarn 2.0. How to prevent yarn 2.0 from installing dev dependencies?
  • zyfyy
    zyfyy over 2 years
    use NODE_ENV=production as environments before excute yarn or yarn install
  • tomdaly
    tomdaly over 2 years
    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
  • Resisty
    Resisty over 2 years
    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.