Ruqayya
Ruqayya

Reputation: 1

deploying Nestjs api to Heroku

when I deploy Nestjs rest api to heroku I don't get any errors but It gets stuck in a loop for over half an hour before I ended the process manually

screenshot of the console

it goes on non-stop

Procfile

web: npm run start:prod

package.json

{
  "name": "rest-api",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "engines": {
    "node": "12.13.1"
  },
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "start":"node dist/src/main.js",
    "prestart:prod": "npm install",
    "start:dist": "node dist/main.js",
    "start:prod": "node dist/main.js",
    "postinstall": "npm run prestart:prod",
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^7.0.0",
    "@nestjs/core": "^7.0.0",
    "@nestjs/jwt": "^7.1.0",
    "@nestjs/passport": "^7.1.0",
    "@nestjs/platform-express": "^7.4.2",
    "@nestjs/typeorm": "^7.1.0",
    "@types/bcrypt": "^3.0.0",
    "bcrypt": "^5.0.0",
    "class-transformer": "^0.3.1",
    "class-validator": "^0.12.2",
    "config": "^3.3.1",
    "multer": "^1.4.2",
    "passport": "^0.4.1",
    "passport-jwt": "^4.0.0",
    "pg": "^8.3.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^6.5.4",
    "typeorm": "^0.2.25"
  },
  "devDependencies": {
    "@nestjs/cli": "^7.0.0",
    "@nestjs/schematics": "^7.0.0",
    "@nestjs/testing": "^7.0.0",
    "@types/express": "^4.17.7",
    "@types/jest": "25.2.3",
    "@types/node": "^13.9.1",
    "@types/supertest": "^2.0.8",
    "@typescript-eslint/eslint-plugin": "3.0.2",
    "@typescript-eslint/parser": "3.0.2",
    "eslint": "7.1.0",
    "eslint-config-prettier": "^6.10.0",
    "eslint-plugin-import": "^2.20.1",
    "jest": "26.0.1",
    "prettier": "^1.19.1",
    "supertest": "^4.0.2",
    "ts-jest": "26.1.0",
    "ts-loader": "^6.2.1",
    "ts-node": "^8.6.2",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^3.7.4"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const serverConfig = config.get('server')
  const app = await NestFactory.create(AppModule);
  app.enableCors();
  const port = process.env.Port|| 3000
  await app.listen(port);
}
bootstrap();

any ideas how to fix this ? I've tried adding some scripts like "heroku-postbuild": "echo Skip builds on Heroku" but also didn't work !

Edit

the problem has been solved , it was because I used a variable in config.yml file that for some reason heroku kept rejecting !

Upvotes: 0

Views: 1578

Answers (1)

Krzysztof Krzeszewski
Krzysztof Krzeszewski

Reputation: 6749

Your config of the postinstall script is incorrect in the package.json

"postinstall": "npm run prestart:prod",
"prestart:prod": "npm install",

What it does is it triggers the npm install all over again. On heroku running install triggers postinstall after it's finished, that includes manually running npm install through a command.

So you run install, which triggers postinstall which triggers another install and so on and so forth.

I'm not sure what you were trying to accomplish with that script, but since it does not really do anything i suggest you simply remove it from package.json.

Upvotes: 1

Related Questions