Skip to content

Instantly share code, notes, and snippets.

@trepichio
Last active September 28, 2021 15:13
Show Gist options
  • Save trepichio/da13505bad43ade52874c4a1d503fa8e to your computer and use it in GitHub Desktop.
Save trepichio/da13505bad43ade52874c4a1d503fa8e to your computer and use it in GitHub Desktop.
Deploy Vue to Heroku
  • 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

Bonus tip:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment