How to get Heroku to recognize a yarn.lock or package.json within a subdirectory (not root)
Asked Answered
T

6

16

I have a Rails application using React, Webpack, and Yarn for the client side. I have everything relating to the client side within the /client directory. This includes my yarn.lock and package.json files. I have a Procfile that cds into /client and properly runs yarn run build. This works locally with no errors. However, when I deploy/push to Heroku, my push is rejected and I get the following error:

remote:        cd client && yarn run build:production
remote:        sh: 1: yarn: not found
remote:        rake aborted!
remote:        Command failed with status (127): [cd client && yarn run build:production...]

What this says to me is that Heroku wasn't able to find a yarn.lock file in the root level. How do I get Heroku to use the yarn.lock file within my /client directory?

Or is the cause of this error something else entirely?

Topping answered 25/3, 2017 at 21:6 Comment(0)
S
15

Just been through a similar thing myself. You need to order the buildpacks so that node is first. If a yarn.lock is in your root dir (or yarn is in your "engines" key as in @remydib's answer), then the node buildpack will install yarn and then your packages. If you have a yarn.lock in /client, put one in root, and add a postinstall script to it that says cd client && yarn run build.

In my case (rake assets:precompile calls yarn and fails), for some reason when the buildpacks go ruby -> node, Heroku doesn't wait for node to install before trying to run rake tasks, and we go kablooie.

Hope this helps.

Saideman answered 27/5, 2017 at 9:14 Comment(2)
It works. But I wonder why we have to put NodeJS buildpacks at the first place?Placia
@Placia The NodeJS Buildpack is needed to make sure that node and npm/yarn are available.Immaterialize
B
17

Make sure to use both ruby and node buildpacks. I got this error when I didn't.

Execute these in your root folder:

heroku buildpacks:set heroku/ruby
heroku buildpacks:add --index 1 heroku/nodejs

Sources: https://devcenter.heroku.com/articles/using-multiple-buildpacks-for-an-app

https://github.com/shakacode/react_on_rails/blob/master/docs/additional-reading/heroku-deployment.md

Batho answered 2/4, 2017 at 16:39 Comment(0)
S
15

Just been through a similar thing myself. You need to order the buildpacks so that node is first. If a yarn.lock is in your root dir (or yarn is in your "engines" key as in @remydib's answer), then the node buildpack will install yarn and then your packages. If you have a yarn.lock in /client, put one in root, and add a postinstall script to it that says cd client && yarn run build.

In my case (rake assets:precompile calls yarn and fails), for some reason when the buildpacks go ruby -> node, Heroku doesn't wait for node to install before trying to run rake tasks, and we go kablooie.

Hope this helps.

Saideman answered 27/5, 2017 at 9:14 Comment(2)
It works. But I wonder why we have to put NodeJS buildpacks at the first place?Placia
@Placia The NodeJS Buildpack is needed to make sure that node and npm/yarn are available.Immaterialize
H
1

maybe your can try typing in package.json

  "engines": {
    "node": "^6.9",
    "yarn": "^0.21"
  },
Hanni answered 28/3, 2017 at 9:55 Comment(0)
G
1
  1. To use yarn to install your application's dependences

    a) yarn.lock should be present in root of the repo. yarn install b) package-lock.json should be removed git rm package-lock.json

  2. To use npm

    a) yarn.lock should be removed

Source: https://devcenter.heroku.com/articles/ruby-support#installed-binaries

https://docs.npmjs.com/files/package-lock.json

Gryphon answered 11/8, 2017 at 6:4 Comment(0)
R
0

I'll add an answer for document another possible use-case. I experienced something similar, multiple buildpacks and I had checked everything in all answers:

  • node-js was declared first in .buildpacks file
  • a yarn.lock was added in the root of the project
  • yarn dependency declared under engines on package.json

Nevertheless yarn was not picking up since I was using an older version of node-js buildpack.


Bumping the version of the buildpack the latest solve my problem:

https://github.com/heroku/heroku-buildpack-nodejs#v118
Ribald answered 26/2, 2018 at 8:20 Comment(0)
C
0

You can keep both package.json and yarn.lock by adding one of these to .slugignore.

It works like .gitignore - it just defines which files to ignore pushing to Heroku.

Check more information here

Calipash answered 10/4, 2018 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.