DEV Community

Saul Hardman
Saul Hardman

Posted on • Originally published at viewsource.io on

How to Configure Yarn on Netlify

Netlify Build is a great platform for running static site builds in conjunction with your Git workflow and Headless CMS content updates (via Webhooks). It makes running cloud-based builds of JavaScript Static Site Generators very slick, but it's not without its pitfalls.

Many of the popular generators use Yarn as a package manager which can be configured via Netlify's netlify.toml configuration file, like so:-

# netlify.toml

[build.environment]
  YARN_VERSION = "1.17.3"
  NODE_ENV = "production"
Enter fullscreen mode Exit fullscreen mode

Note: the "Yarn approved" method of specifying the required version is via the yarn policies set-version command.

Running the build step in an environment other than local development had me wondering whether to install packages as dependencies or devDependencies. With the environment variable NODE_ENV set to "production" Yarn will only install dependencies specified in the dependencies section of the package.json file, and not those within devDependencies.

Perhaps NODE_ENV could be set to "production" within the scope of the build command? For example:-

{
  "scripts": {
    "build": "NODE_ENV=production ..."
  }
}
Enter fullscreen mode Exit fullscreen mode

The cleanest solution (in my humble opinion) is to set the --production flag to false, like so:-

# netlify.toml

[build.environment]
  YARN_VERSION = "1.17.3"
  YARN_FLAGS = "--production=false"
  NODE_ENV = "production"
Enter fullscreen mode Exit fullscreen mode

You can find out more about configuring build environment variables within the Netlify documentation.

Latest comments (0)