Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku deploy using ts-node

I am trying to deploy a sample of the Angular Universal Starter in Heroku.

The task npm start fails because it does not recognize ts-node.

Is there a way to make deployment on Heroku work using ts-node?

like image 227
Joao Garin Avatar asked Dec 11 '15 12:12

Joao Garin


5 Answers

You can create a Procfile with

web: npm start

And have your start script to run ts-node

"start": "ts-node src/index.ts",

And install typescript and ts-node on your dependencies

"ts-node": "^3.3.0",
"typescript": "^2.4.2"
like image 154
BrunoLM Avatar answered Oct 13 '22 16:10

BrunoLM


package.json :

"start":"ts-node index.ts",
"dependencies": { "ts-node" : "~<version number>" }

work for me :)

like image 30
nadav Avatar answered Oct 13 '22 16:10

nadav


You can't deploy ts-node to heroku directly. either build your own buildpack or just compile typescript to javascript. I recommend the latter. Just run the command tsc -p . on publish.

Edit You can also create a file called index.js and add the following:

require('ts-node/register');
require('./server.ts');

Then it should work. Don't forget to run npm install --save-dev ts-node as well.

This is not recommended in production environment. Use it only in development.

like image 28
Louay Alakkad Avatar answered Oct 13 '22 18:10

Louay Alakkad


To resolve problem in heroku related to missing ts-node you need to build your TypeScript code using tsc command in your package.json.

 "scripts": {
    ...
    "build": "tsc",
  }

Then in Procfile you can use node command to run files that Heroku build for you by ruunning build command in package.json.

web: node dist/src/server.js

Personally, I think that's a best solution for deployment a TypeScript-based application to Heroku, if you need also tsconfig.json you can take a look for mine.

{
  "extends": "@araclx/tsconfig",
  "compilerOptions": {
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "noUnusedParameters": false,
    "noUnusedLocals": false,
    "lib": ["ES2018"],
    "module": "commonjs",
    "target": "es6",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist"
  }
}
like image 2
keinsell Avatar answered Oct 13 '22 18:10

keinsell


Maybe I'm late, but I was running into the same issue this week and this is what I've done to solve the issue by gathering multiple solutions in the web. Hope it helps someone:

Added the node and yarn or npm versions to package.json

  "engines": {
    "node": "12.14.0",
    "yarn": "1.x"
  },

Added ts-node and typescript to the project dependencies on the package.json

"ts-node": "^9.0.0",
"typescript": "^4.1.2"

Added the postinstall script that is called after the yarn or npm install called by Heroku, this will build your app on the chosen destination folder (dist in this case)

And changed the start script to server the compiled app instead of the TS one

  "scripts": {
    "postinstall": "tsc",
    "start": "cross-env NODE_ENV=production node dist/index.js"
  },

Pointed to the start script on the PROCFILE

web: yarn start

And finally this is the tsconfig.json where you can choose the build destination "outDir": "dist",

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "target": "es2018",
        "moduleResolution": "node",
        "module": "commonjs",
        "sourceMap": true,
        "rootDir": "src",
        "outDir": "dist",
        "lib": ["esnext", "dom", "es2018", "esnext.asynciterable"],
        "esModuleInterop": true
    },
    "exclude": ["node_modules"]
}
like image 2
Candous Avatar answered Oct 13 '22 18:10

Candous