How I deployed Codesandbox Apps to Heroku
Tags: react, heroku, node, codesandbox
Published: 2019-05-19 06:21:04

---
title: How I deployed Codesandbox Apps to Heroku
published: true
description: You can now deploy your Codesandbox apps to Heroku right from the editor with my Codesandbox fork.
tags: react, heroku, nodejs, codesandbox
cover_image: https://thepracticaldev.s3.amazonaws.com/i/ods3gwfj1xeytmy7a7kr.png
---

I was discussing with a friend when we paired code on Codesandbox and trying to deploy to Heroku, we talked about on how far the web has gone with the help of JavaScript and how everything will end up running from the browser.

Yes, on Codesandbox, you can pretty much do anything you can on Visual Studio code or any other code editor. Another good thing is you can directly deploy to Zeit, Netlify and now Heroku on my Codesandbox fork with custom deploy scripts.

In one of my recent tweets, I tweeted the ability to deploy to Heroku, I also talked about the modes of deployments possible with Heroku.

{% twitter 1129378697188990976 %}

## Modes of Deployment

### Deploy to Heroku URL

This is the first and simplest method in deploying sandbox to Heroku. Heroku pretty much made it easier for users to deploy by using a URL whereby you provide a template which is the git repo URL of the code you are trying to deploy as a URL parameter and the repo must include an app.json for your app settings. You must also be logged in to your Heroku account before you can deploy anything.

Below is an example of the url format:

```
https://heroku.com/deploy?template=<github_url>
```

Here's a sample app.json file to deploy a react app:

```
{
  "name": "React Bare App",
  "description": "A barebones React app",
  "repository": "https://github.com/CITGuru/newTapp",
  "logo": "http://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/220px-React-icon.svg.png",
  "keywords": ["node", "react", "static"],
  "buildpacks": [
    {
      "url": "heroku/nodejs"
    },
    {
      "url": "https://github.com/heroku/heroku-buildpack-static.git"
    }
  ],
  "scripts": {
    "postdeploy": "npm run build",
  }
}
```

You check out more information about the [app.json schema](https://devcenter.heroku.com/articles/app-json-schema) documentation.

This method is very easy to implement if you know your way around Heroku and it's currently the supported and ready to use mode in my fork.

### Using Heroku Git CLI

This is the second mode that requires a special server to host your sandbox by providing a GitHub url or by a zipped of your code provided by Codesandbox. I am currently working on a Go server that performs this deploy tasks and is able to send back logs. With this method, you wouldn't need to leave Codesandbox or even push your code to GitHub to deploy your code. 

The backdrop is you'd have to provide your Heroku login details since the go server will be using this to deploy your code. And this will be asked everytime you wanted to deploy for security reasons.

#### How the Go server works

It's basically a basic HTTP server that accepts a request. You can either send a request with your zipped code or a Github url and your Heroku login details. It already has predefined commands to better deploy your app even with custom build scripts you defined in your app.json or even Procfile. It uses both Heroku and Git to perform these tasks.

*<b>I pretty much don't know if this is the best way, but this will work for sure</b>*

## How can I Deploy sandbox to Heroku now

Now, lets get to business and see how we can deploy a sandbox to Heroku using my Consandbox fork.

Go ahead and fork/clone my [Codesandbox fork](https://github.com/CITGuru/codesandbox-client) and follow the [contribution readme](https://github.com/CITGuru/codesandbox-client/blob/master/CONTRIBUTING.md) to set it up for local use. Ensure you have an account on Codesandbox and login to your account locally (check the contribution guide to know how to).

Once you are done setting up and logged in your account with that, you are good to go. 

Create a new react sandbox, after that click on the more button, then click on **Fork sandbox**.

![](https://thepracticaldev.s3.amazonaws.com/i/m8q6sh9nt05ktv8vxox6.png)
![](https://thepracticaldev.s3.amazonaws.com/i/ikgjvh5fp4j0fw2yk5he.png)

Add a new file called app.json with below settings:

```json
{
  "name": "myaspp",
  "logo": "http://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/220px-React-icon.svg.png",
  "buildpacks": [
    {
      "url": "heroku/nodejs"
    },
    {
      "url": "https://github.com/heroku/heroku-buildpack-static.git"
    }
  ],
  "scripts": {
    "postdeploy": "npm run build"
  }
}

```

Above is the settings we'll use to deploy our new react app. Now let's explain what we have in our app.json:

**name**: Name of the app.
**logo**: The logo of the app. It shows up in favicon.
**buildpacks**: This is a very important settings that help us define our app buildpacks. The `heroku/nodejs` is an official Heroku buildpacks that runs nodejs apps on Heroku, while `https://github.com/heroku/heroku-buildpack-static.git` is an unofficial Heroku buildpacks for running static files by providing the build the folder.
**scripts**: This also very crucial because with this we are able to run custom commands and build scripts before Heroku deploys your app. `postdeploy` is the command entry of scripts, so ensure your other commands are being called from there.

![](https://thepracticaldev.s3.amazonaws.com/i/4kcnxyortq7z7cb4sd37.png)

There are many other things you can define in app.json like your environment variables,add-ons, docker-images, dyno and other things you can customize in Heroku.

You can also add a `Procfile` for custom dyno processes.

Now we need to add a settings file `static.json` for `heroku-build-static`:

```json
{
  "root": "build/"
}
```

This is used to tell `heroku-build-static` where it can find our build files. The `heroku-build-static` comes with an express server that serves our static files in the build folders.

The next thing to do now is to deploy, but we need to push our code to GitHub first from Codesandbox. 

So click on the GitHub icon, enter any name and click on create repository to push the code to GitHub. 

![](https://thepracticaldev.s3.amazonaws.com/i/9idp31ecg5q8tyj62ebe.png)

Once thats done, click on the more button and click on fork sandbox.

![](https://thepracticaldev.s3.amazonaws.com/i/el8uu4u6onvkk50pfmgk.png)

After this, then we can now deploy our app by using the new deploy to Heroku feature. 

Click on deployment, you should see something like below, click on Heroku and click on deploy.

![](https://thepracticaldev.s3.amazonaws.com/i/2zl6g5nmc84leev5966i.png)
![](https://thepracticaldev.s3.amazonaws.com/i/7o523sdtub38mumrne6s.png)

There will be a popup, click on deploy, and you'll be taken to Heroku page where you will start the deploy process and see your app build logs.

![](https://thepracticaldev.s3.amazonaws.com/i/qab27omri9p8rupzzc78.png)

If you are not logged in to Heroku, you will be prompted to log in your Heroku account.

Once the page opens up, enter the URL you preferred and click on Deploy app.

![](https://thepracticaldev.s3.amazonaws.com/i/o66l0yzk9061liqdm177.png)

Then wait to see your app deploys and build logs and processes.

![](https://thepracticaldev.s3.amazonaws.com/i/kz9ufmac7txarfqkp893.png)

Tada, and it's successful!

![](https://thepracticaldev.s3.amazonaws.com/i/kekvtlasa00yqkyps4jx.png)

Here's our app: https://kkgugjh.herokuapp.com/

I have tried deploying a vue and angular app with this method and all is good. 

Like I said earlier if you know your way around Heroku this will be extremely easy.

<b>I have come to love Codesandbox and would love to be on their development team.</b>

I will probably write more articles on Codesandbox, because I also have a different fork that I am working on to be able to workon Django apps and maybe Ruby later. But that's still far fetched and not enough time to work on it.

<b>If you enjoyed this article, please heart and share.</b>