将Nestjs api部署到Heroku

时间:2020-08-13 08:32:01

标签: typescript heroku nestjs

当我将Nestjs rest api部署到heroku时,我没有收到任何错误,但是在我手动结束该过程之前,它陷入了一个半小时的循环中

screenshot of the console

它不停地

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();

有什么解决办法吗? 我尝试添加一些脚本,例如 “ heroku-postbuild”:“回显在Heroku上构建” 但也没用!

修改

问题已解决,这是因为我在config.yml文件中使用了一个变量,出于某种原因,heroku一直拒绝使用它!

1 个答案:

答案 0 :(得分:0)

您在package.json

中的后安装脚本配置不正确
"postinstall": "npm run prestart:prod",
"prestart:prod": "npm install",

它的作用是再次触发npm install。 在heroku上,正在运行的安装会在完成后触发安装后安装,其中包括通过命令手动运行npm install

因此,您运行安装,这将触发安装后,然后触发另一次安装,依此类推。

我不确定您要使用该脚本完成什么操作,但是由于它实际上并没有执行任何操作,因此我建议您将其从package.json中删除。