Node.js 如何将nestjs应用程序(typescript)部署到heroku?

Node.js 如何将nestjs应用程序(typescript)部署到heroku?,node.js,typescript,heroku,nestjs,Node.js,Typescript,Heroku,Nestjs,我创建了一个nestjs应用程序,现在我正在寻找在prod环境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

我创建了一个nestjs应用程序,现在我正在寻找在prod环境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
我意识到节点ts不是由heroku安装的。 在本地生成js文件并将其推送到heroku可以解决这个问题吗? 还是有更好的解决方案

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

如果您查看一下您的
包.json
,您将看到两个属性:
依赖项
devdependency
。在Heroku上,前者用于开发和生产环境,后者仅用于开发


要构建应用程序,您需要
ts节点
依赖项,它位于
devdependences
中。您需要将其移动到您的
依赖项中
才能安装并在生产环境中构建应用程序。

第二个解决方案可能是为
安装后
创建新脚本,该脚本将运行
tsc
,如下所示:

"postinstall": "tsc --sourceMap false"
您还需要更改
start
脚本以使用
node
启动应用程序:

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

因为heroku只安装生产依赖项,您也可以检查也可以检查(2021+的实际值)。