How to setup Heroku Rails app to handle yarn.lock

One of the nicest features of Rails 5 is its integration with [Yarn](https://github.com/yarnpkg/yarn), the latest and greatest package manager for Node.js. Using it means you can install JavaScript dependencies for your app just as easily as you use Bundler to install Ruby gems.

Now one of the biggest problems you face when using any sort of Node package management is that the combinatorial explosion of libraries downloaded in order to do anything of significance.

![](http://devhumor.com/content/uploads/images/July2017/npm_package.png)

Given that reality, you really do _not_ want to add `node_modules` to your project's git repository, no more than you would want to add all the source code of your gems. Instead, you add `node_modules` to your `.gitignore` file.

Yarn adds a file to the root of your Rails app called `yarn.lock`. Today I learned that if you include the Node.js buildpack to your project on Heroku, it will recognize `yarn.lock` and install any required node modules for you. You just have to make sure that it [runs first in the build chain](https://stackoverflow.com/questions/43021956/how-to-get-heroku-to-recognize-a-yarn-lock-or-package-json-within-a-subdirectory/44215065#44215065).

`heroku buildpacks:add --index 1 heroku/nodejs`

Side note: If you use Heroku CI then you'll need to setup your test environment with the extra buildpack also by adding a new section to `app.json`.

```
"buildpacks": [
{ "url": "heroku/nodejs" },
{ "url": "heroku/ruby" }
]
```

Note that the nodejs buildpack expects a `test` script to be present in `package.json`. If you don't have one already, just add a dummy directive there. Almost anything will work; I just put an echo statement.

```
"scripts": {
    "test": "echo 'no tests in js'"
  },
```

obie-fernandez
August 21, 2017
