gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:34:26 27 4
gpt4 key购买 nike

我创建了一个 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。在本地生成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"
}
}

最佳答案

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

要构建您的应用程序,您需要 devDependencies 中的 ts-node 依赖项。您需要将它移动到您的依赖项以安装它并在生产环境中构建您的应用程序。

关于node.js - 如何将 nestjs 应用程序( typescript )部署到 heroku?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53957016/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com