DEV Community

loading...
Cover image for Volta + Netlify

Volta + Netlify

michalbryxi profile image Michal Bryxí ・2 min read

Volta

On my frontend projects I like to use Volta to pin down node and yarn versions to make sure that it builds the same every time. It's as simple as:

# install volta via: https://volta.sh/

❯ volta pin node
success: pinned node@14.15.4 (with npm@6.14.10) in package.json

❯ volta pin yarn
success: pinned yarn@1.22.5 in package.json
Enter fullscreen mode Exit fullscreen mode

And from now on every environment that has volta installed respective versions of node and yarn will be used. Automatically, no manual switching necessary.

The problem

Unfortunately my favourite frontend hosting service - Netlify does not support volta out of the box. Which I found the hard way after a project update. Although it compiled fine on localhost, it refused to work with the default version of node on netlify.

Solution

After a little bit of googling I found an-OK solution in Netlify file based configuration. The file netlify.toml goes to the root of your repo:

# netlify.toml
[build.environment]
  NODE_VERSION = "14.15.4"
  YARN_VERSION = "1.22.5"
Enter fullscreen mode Exit fullscreen mode

Conclusion

  • Different (major) versions of node can be incompatible and code running fine under one might fail to build under other.
  • If you want to pin node and yarn version for netlify, use netlify.toml config.

Photo by Scott Sanker on Unsplash

Discussion (1)

pic
Editor guide
Collapse
gnclmorais profile image
Gonçalo Morais

Bonus points: run this and you’ll have in your clipboard (ready to paste on your netfily.toml both Node and Yarn versions:

jq -r '.volta | to_entries | map("  \(.key | ascii_upcase)_VERSION = \"\(.value|tostring)\"")|.[]' package.json | pbcopy
Enter fullscreen mode Exit fullscreen mode