How do I compile Typescript at Heroku postinstall?

Typescript should be installed as a devdependency

have web: node server.js in your procfile

make sure to add npm build as a postinstall script

Telling npm that typescript is installed locally will fix tsc not found issue, since npm is trying to find it globally on heroku.

like this.

"tsc": "./node_modules/typescript/bin/tsc",
"build": "tsc",
"postinstall": "npm run build",

Spent a while to deploy my simple typescript create-react-app to Heroku. Here what worked for me.

package.json - does not need postinstall at all

In the command line install buildpack for your application run: heroku buildpacks:add zidizei/typescript heroku buildpacks:add heroku/nodejs

You can also search for buildpacks run: heroku buildpacks:search typescript

My server looks like that (/root/server.js)

const express = require('express')
const app = express()
const PORT = process.env.PORT || 3000

app.use(express.static('build'));
app.listen(PORT, () => console.log(`Listening on port ${PORT}`))

package.json

 "scripts": {
    "start": "node server.js",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  }

Also before pushing to heroku, do run 'npm run build'.

My solution wont work if you gonna use webpack dev server, it must be custom server, in my case it was EXPRESS.


You need to call tsc from an npm script. Otherwise Heroku tries to find a global dependency named tsc.

Create a new npm script in your package.json:

"tsc": "tsc"

now replace "postinstall": "tsc" with:

"postinstall": "npm run tsc"