Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I deploy my Typescript Node.js app to Heroku?

When testing locally I was previously running:

"build-live": "nodemon --exec ./node_modules/.bin/ts-node -r dotenv/config -- ./index.ts"

I then figured my Procfile should be something like:

web: ./node_modules/.bin/ts-node -- ./index.ts

But it says module 'typescript' not found, even when it is in package.json. I read in a few places that ts-node is not the way to go to deploy to Heroku, so I am not sure what to do.

UPDATE: I think I am supposed to compile it, so I tried:

web: ./node_modules/.bin/tsc --module commonjs --allowJs --outDir build/ --sourceMap --target es6 index.ts && node build/index.js

This succeeds, however when actually running it, a bunch of the libs I'm using get "Cannot find module '...'".

like image 359
atkayla Avatar asked Apr 10 '17 08:04

atkayla


People also ask

How do I deploy a node app in Heroku?

Step 4: Deploying the Node.js app to HerokuClick on the “Create new app”. Open the Deploy tab and scroll to the “Deployment method” section of the tab. Select GitHub as a method. It will show a “Connect to GitHub” option where we add provide our GitHub repository.


2 Answers

Alternatively you can have the TypeScript compile as a postinstall hook and run node build/index.js as the only Procfile command:

Your package.json should contain a postinstall hint that gets executed after npm install and before the node process launches:

"scripts": {
  "start": "node build/index.js",
  "build": "tsc",
  "postinstall": "npm run build"
}

You can then leave your Procfile as is:

web: npm start

This 'build on deploy' approach is documented by Heroku here.

like image 86
Fabian Avatar answered Sep 19 '22 14:09

Fabian


My problem was about missing Typescript npm modules. The Typescript compiler tsc was not found when deployed the app to Heroku.

The Heroku deploy process (rightly) does not install development dependencies, in my case the Typescript module was part of devDependencies and thus the tsc command was not running on the Heroku platform.

Solution 1

Add typescript to dependencies: npm i typescript -s

Solution 2

  • Open Heroku console:

Heroku console

  • Select console type:

Select Heroku console type

  • Run the command npm i typescript && npm run tsc
like image 38
xameeramir Avatar answered Sep 21 '22 14:09

xameeramir