How to include Node major version in cache key


On a build using the ruby:2.4.2-node-browsers image, am currently caching my node_modules using this key, as recommended : v1-project-yarn-{{ arch }}-{{ checksum "yarn.lock" }}

But the node_modules folder needs to be different for major Node versions. In my case, I have issues with builds on Node 9 using node_modules from Node 8 because of pre-compiled NPM packages (like node-sass).

The fix would be to include the major node version (8, 9, …) in the cache key, but what is the best way to do so? Should I set an environment variable containing this version, and use it in the cache key?

This is a common use case (also valid for major Ruby versions for example), it should be mentionned in the docs. A nice way to fix this could be to provide an env variable in the CircleCI docker images with the major version, like NODE_MAJOR_VERSION and RUBY_MAJOR_VERSION and advise to use it in the cache key in the docs.


There shouldn’t be a need to switch Node versions frequently. But if you had to cache on something like that, you could node --version > node_version and reference it in the cache key like {{checksum "node_version"}}.

You can go through your build history to find a node 8 version of that image and put the image sha in your configuration. The image will never, ever change if you do that.


This is what I did. I added an initial step which generates this file and persists it to the workspace: Nodejs version


This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.