如何将nestjs应用程序(打字稿)部署到heroku?

时间:2018-12-28 10:26:15

标签: node.js typescript heroku nestjs

我创建了一个nestjs应用,现在我正在寻找在产品环境Heroku中部署它的最佳方法。

当我尝试部署nest-cli生成的代码时,我从heroku获得了以下日志:

2018-12-28T08:37:23.881261+00:00 app[api]: Release v1 created by user myemail@gmail.com
2018-12-28T08:37:24.051831+00:00 app[api]: Release v2 created by user myemail@gmail.com
2018-12-28T08:37:23.881261+00:00 app[api]: Initial release by user myemail@gmail.com
2018-12-28T09:00:47.000000+00:00 app[api]: Build started by user myemail@gmail.com
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]: > nest-app-heroku@0.0.0 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! nest-app-heroku@0.0.0 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 nest-app-heroku@0.0.0 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 marouen.dbouba@gmail.com
2018-12-28T09:01:33.022359+00:00 app[api]: Release v3 created by user marouen.dbouba@gmail.com
2018-12-28T09:01:33.039917+00:00 app[api]: Scaled to web@1:Free by user marouen.dbouba@gmail.com
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]: > nest-app-heroku@0.0.0 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

我知道的是heroku没有安装node-ts。 Doest在本地生成js文件并将其推送到heroku是否可以解决问题? 还是有更好的解决方案?

这是我的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"
  }
}

2 个答案:

答案 0 :(得分:1)

如果您查看package.json,将看到两个属性:dependenciesdevDependencies。在Heroku上,前者用于开发和生产环境,而后者仅用于开发。

要构建应用,您需要ts-node中的devDependencies依赖项。您需要将其移至dependencies进行安装并在生产环境中构建应用。

答案 1 :(得分:1)

第二个解决方案可能是为postinstall创建新脚本,该脚本将像这样运行tsc

"postinstall": "tsc --sourceMap false"

另外,您需要更改start脚本以使用node来启动应用程序:

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