7

I use the following buildpack: heroku-buildpack-nodejs And by default, it should cache and restore node_modules.

yarn.lock file is located at the root of the application along with package.json.

I've also added "cacheDirectories": [".cache/yarn"] to package.json

During the review-app build log analysis, I see:

-----> Restoring cache
Loading 1 from cacheDirectories (package.json):
- .cache/yarn (not cached - skipping)

...

-----> Installing dependencies
       Installing node modules (yarn.lock)

...

-----> Caching build
       - node_modules

Seems like this issue was opened on Dec 20, 2016 : https://github.com/heroku/heroku-buildpack-nodejs/issues/359

How can I achieve the caching mechanism without installing the dependencies on every build?

CoryCoolguy
  • 1,065
  • 8
  • 18
Oron Bendavid
  • 1,485
  • 3
  • 18
  • 34

1 Answers1

0

v83 of Heroku NodeJS build pack has no support for installing dependencies with yarn. This was added in v93.

Buildpack v93 is not without its own issues. Since it hard codes the yarn cache path relative to $build_dir as $build_dir/.cache/yarn/v2. This presents an issue as $build_dir is a temporary directory and can't be relied to exist in the next deployment.

To demonstrate this, add a heroku-postbuild or heroku-prebuild command script for scripts key in package.json to show the folder deployment is running in.

"heroku-postbuild": "pwd"

Part output of build logs

remote:        $ echo 'postbuild'; pwd
remote:        postbuild
remote:        /tmp/build_260cb9d4ad95c7671332f8a404117b59
remote:        Done in 0.10s.

Some issues with yarn deployments are addressed in later version of the buildpack. I recommend using the latest build pack which currently is v165

In v165, the cache is copied from the temporary folder where yarn caches dependencies to that configured for cacheDirectories key in package.json

Currently, this feature is behind a feature flag.

In order to enable it for your deployment, create a features file in your project.

.
├── README.md
├── features
├── package.json
└── yarn.lock

In features file, write

cache-native-yarn-cache=true

Re-running your deployment should produce a log similar with:

remote:
remote: -----> Restoring cache
remote:        Loading 1 from cacheDirectories (package.json):
remote:        - .cache/yarn
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81