maroodb
maroodb

Reputation: 1108

How to deploy nestjs app (typescript) to heroku?

I created a nestjs app and now I am looking for the best way to deploy it in a prod environnemnt Heroku.

When I tried to deploy the code as it is generated by nest-cli I got this logs from heroku :

2018-12-28T08:37:23.881261+00:00 app[api]: Release v1 created by user [email protected]
2018-12-28T08:37:24.051831+00:00 app[api]: Release v2 created by user [email protected]
2018-12-28T08:37:23.881261+00:00 app[api]: Initial release by user [email protected]
2018-12-28T09:00:47.000000+00:00 app[api]: Build started by user [email protected]
2018-12-28T09:01:37.401065+00:00 heroku[web.1]: Starting process with command `npm start`
2018-12-28T09:01:40.164685+00:00 heroku[web.1]: Process exited with status 1
2018-12-28T09:01:40.205293+00:00 heroku[web.1]: State changed from starting to crashed
2018-12-28T09:01:40.209626+00:00 heroku[web.1]: State changed from crashed to starting
2018-12-28T09:01:40.051608+00:00 app[web.1]:
2018-12-28T09:01:40.051626+00:00 app[web.1]: > [email protected] start /app
2018-12-28T09:01:40.051628+00:00 app[web.1]: > ts-node -r tsconfig-paths/register src/main.ts
2018-12-28T09:01:40.051630+00:00 app[web.1]:
2018-12-28T09:01:40.055835+00:00 app[web.1]: sh: 1: ts-node: not found
2018-12-28T09:01:40.060789+00:00 app[web.1]: npm ERR! file sh
2018-12-28T09:01:40.061057+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2018-12-28T09:01:40.061266+00:00 app[web.1]: npm ERR! errno ENOENT
2018-12-28T09:01:40.061453+00:00 app[web.1]: npm ERR! syscall spawn
2018-12-28T09:01:40.062961+00:00 app[web.1]: npm ERR! [email protected] start: `ts-node -r tsconfig-paths/register src/main.ts`
2018-12-28T09:01:40.063106+00:00 app[web.1]: npm ERR! spawn ENOENT
2018-12-28T09:01:40.063357+00:00 app[web.1]: npm ERR!
2018-12-28T09:01:40.063536+00:00 app[web.1]: npm ERR! Failed at the [email protected] start script.
2018-12-28T09:01:40.063679+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2018-12-28T09:01:40.071632+00:00 app[web.1]:
2018-12-28T09:01:40.071873+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-12-28T09:01:40.072032+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2018-12-28T09_01_40_065Z-debug.log
2018-12-28T09:01:33.022359+00:00 app[api]: Deploy 9c1f4f41 by user [email protected]
2018-12-28T09:01:33.022359+00:00 app[api]: Release v3 created by user [email protected]
2018-12-28T09:01:33.039917+00:00 app[api]: Scaled to web@1:Free by user [email protected]
2018-12-28T09:01:44.674047+00:00 heroku[web.1]: Starting process with command `npm start`
2018-12-28T09:01:48.017743+00:00 heroku[web.1]: State changed from starting to crashed
2018-12-28T09:01:47.991814+00:00 heroku[web.1]: Process exited with status 1
2018-12-28T09:01:47.850020+00:00 app[web.1]:
2018-12-28T09:01:47.850041+00:00 app[web.1]: > [email protected] start /app
2018-12-28T09:01:47.850043+00:00 app[web.1]: > ts-node -r tsconfig-paths/register src/main.ts
2018-12-28T09:01:47.850044+00:00 app[web.1]:
2018-12-28T09:01:47.864981+00:00 app[web.1]: sh: 1: ts-node: not found
2018-12-28T09:01:47.880409+00:00 app[web.1]: npm ERR! file sh

what i realise is that node-ts is not installed by heroku. Doest generating js file locally and push it to heroku can solve the problem ? or there is a better solution ?

this my package.json:

{
  "name": "nest-app-heroku",
  "version": "0.0.0",
  "description": "description",
  "author": "",
  "license": "MIT",
  "scripts": {
    "format": "prettier --write \"src/**/*.ts\"",
    "start": "ts-node -r tsconfig-paths/register src/main.ts",
    "start:dev": "nodemon",
    "start:debug": "nodemon --config nodemon-debug.json",
    "prestart:prod": "rimraf dist && tsc",
    "start:prod": "node dist/main.js",
    "start:hmr": "node dist/server",
    "lint": "tslint -p tsconfig.json -c tslint.json",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:e2e": "jest --config ./test/jest-e2e.json",
    "webpack": "webpack --config webpack.config.js"
  },
  "dependencies": {
    "@nestjs/common": "^5.1.0",
    "@nestjs/core": "^5.1.0",
    "reflect-metadata": "^0.1.12",
    "rxjs": "^6.2.2",
    "typescript": "^3.0.1"
  },
  "devDependencies": {
    "@nestjs/testing": "^5.1.0",
    "@types/express": "^4.16.0",
    "@types/jest": "^23.3.1",
    "@types/node": "^10.7.1",
    "@types/supertest": "^2.0.5",
    "jest": "^23.5.0",
    "nodemon": "^1.18.3",
    "prettier": "^1.14.2",
    "rimraf": "^2.6.2",
    "supertest": "^3.1.0",
    "ts-jest": "^23.1.3",
    "ts-loader": "^4.4.2",
    "ts-node": "^7.0.1",
    "tsconfig-paths": "^3.5.0",
    "tslint": "5.11.0",
    "webpack": "^4.16.5",
    "webpack-cli": "^3.1.0",
    "webpack-node-externals": "^1.7.2"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Upvotes: 3

Views: 7571

Answers (2)

Baboo
Baboo

Reputation: 4258

If you have a look at your package.json, you will see two properties: dependencies and devDependencies. On Heroku, the former is used for development and production environments and the latter is used for development only.

To build your app, you need the ts-node dependency which is in your devDependencies. You need to move it to your dependencies to have it installed and to build your app on the production environment.

Upvotes: 8

Ivan Vasiljevic
Ivan Vasiljevic

Reputation: 5718

Second solution could be to create new script for postinstall which will run tsc like this:

"postinstall": "tsc --sourceMap false"

Also you need to change start script to use node for starting application:

"start": "node dist/main.js",

Upvotes: 6

Related Questions