Why isn't my build using the cached node modules?

Issue

When a user is using yarn to perform build tasks, it may appear that yarn is reinstalling node modules even though the build is expected to be using the Heroku cache.

Resolution

Check if NODE_MODULES_CACHE is true. If it is, the build is using the Heroku build cache. This is cached from build to build to be reused.

If it is false or blank, set it to true:

heroku config:set NODE_MODULES_CACHE=true -a APP_NAME

When NODE_MODULES_CACHE is true, the buildpack will copy the node modules from the app directory ($build_directory/node_modules) to the Heroku cache at the end of the build. On the next build, the buildpack will restore those node modules to the app directory, then it will run yarn install and use the yarn.lock to resolve dependencies.

Monorepos

If there are multiple node_modules directories (for example, a directory for server and a directory for ui), the app will need to use cacheDirectories to customize for multiple node modules cache directories.

Please read more about this in our Dev Center:
https://devcenter.heroku.com/articles/nodejs-support#custom-caching

Ask on Stack Overflow

Engage with a community of passionate experts to get the answers you need

Ask on Stack Overflow

Heroku Support

Create a support ticket and our support experts will get back to you

Contact Heroku Support