28

I'm trying to deploy a node app, but I'm running into a problem with how my code is structure. The app is git inited at the top level and it looks like:

App (git tracked in remote repo)
- .git
- server
- client
- plugin
- extras

Since Heroku needs me to push only the server directory directly with the package.json build file, I'm not sure how to setup my directory. I want it such that I can deploy heroku from the 'App/server' folder, but still git pull/push the entire app as I do now:

App (git tracked in remote repo)
- .git
- server (can run 'git push heroku master' for just this folder)
  - .git (?)
- client
- plugin
- extras

How I can achieve this the simplest? I read about git-submodules, but that seems messy and I wanted to be sure. Thank you very much from a git nub.

kaid
  • 1,249
  • 2
  • 16
  • 32

3 Answers3

38

I think git-subtree should work:

git subtree push --prefix server heroku master

Additional resources:

Community
  • 1
  • 1
rgbrgb
  • 1,146
  • 1
  • 11
  • 17
2

Heroku requires a package.json and a lockfile in the root of the repository or they will fail the deploy.

However you can set up a package.json in the root of the repo which installs dependencies for the subdirectories and runs their related commands.

Using yarn for example, in your case you could put an empty yarn.lock in the root and a package.json like:

{
  "name": "my-project",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "postinstall": "yarn --cwd server --production=false install",
    "build": "yarn --cwd server serve"
  }
}

The --production=false ensures that devDependencies will be installed, which otherwise wouldn't happen since NODE_ENV is set to production in Heroku's environment.

Matt Sanders
  • 8,023
  • 3
  • 37
  • 49
1

Alternatively, you can use git subtree to create a heroku branch on GitHub:

git subtree push --prefix server origin heroku

which you can then deploy to Heroku using a Heroku Button. Just add an app.json and a README.md with the button to your server directory:

[![Deploy](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy)

Deploy

and let Heroku take care of the rest.

Ron Inbar
  • 2,044
  • 1
  • 16
  • 26
  • Can you please share more about how to get this button to work in a subfolder? I've got the app.json and README.md in my subfolder, but the link still tries to get app.json from the root directory – Scott Carlson May 08 '21 at 00:51
  • 1
    @ScottCarlson In your main `README.md`, change the button markdown to `[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/roninbar/kwik-e-mart/tree/heroku)` (replace `roninbar/kwik-e-mart` with your GitHub username/repo). – Ron Inbar May 08 '21 at 12:01
  • @ScottCarlson Alternatively, switch to the `heroku` branch in GitHub before clicking the button. – Ron Inbar May 08 '21 at 12:10