- Add a node server to serve your files. For this we need a simple express server like this one
const express = require('express');
const serveStatic = require("serve-static")
const path = require('path');
app = express();
app.use(serveStatic(path.join(__dirname, 'dist')));
const port = process.env.PORT || 80;
app.listen(port);
Make sure you npm install express serve-static
and save it in your project root under server.js
or another name that you prefer.
- Add package.json scripts to start the node server
"postinstall": "npm run build",
"start": "node server.js",
- Add heroku remote and push to it
$ git remote add heroku git_url_here
$ git push heroku master
Since postinstall command will run whenever you add a dependency, you might be annoyed by the fact that a build is run after installing a dependency.
We can easily fix that by running postinstall conditionally like so
"postinstall": "if test \"$NODE_ENV\" = \"production\" ; then npm run build ; fi ",copy
This will conditionally run our postinstall script only if NODE_ENV is set to production. We can set node env in heroku very easily in the settings tab like so