[英]Nest.JS deploy to Heroku
I'm currently trying to deploy the basic starter template generated by the Nest.JS CLI (as of version 5.3.0) and seem to be getting a timeout on app startup.我目前正在尝试部署由 Nest.JS CLI(从 5.3.0 版开始)生成的基本启动器模板,并且似乎在应用程序启动时超时。 I'm wondering if anyone has managed to deploy to Heroku?我想知道是否有人设法部署到 Heroku?
I'm not sure whether or not I need to include some kind of Procfile?我不确定是否需要包含某种 Procfile?
Also, there doesn't seem to be much info around deploying the Nest.JS此外,关于部署 Nest.JS 的信息似乎并不多
The Heroku logs when I try to deploy.我尝试部署时的 Heroku 日志。
heroku[web.1]: Starting process with command `npm start`
app[web.1]:
app[web.1]: > testy@0.0.0 start /app
app[web.1]: > ts-node -r tsconfig-paths/register src/main.ts
app[web.1]:
app[web.1]: [Nest] 21 - 2018-10-16 06:52:17 [NestFactory] Starting Nest application...
app[web.1]: [Nest] 21 - 2018-10-16 06:52:17 [InstanceLoader] AppModule dependencies initialized +21ms
app[web.1]: [Nest] 21 - 2018-10-16 06:52:17 [RoutesResolver] AppController {/}: +48ms
app[web.1]: [Nest] 21 - 2018-10-16 06:52:17 [RouterExplorer] Mapped {/, GET} route +7ms
app[web.1]: [Nest] 21 - 2018-10-16 06:52:17 [NestApplication] Nest application successfully started +3ms
app[web.1]: Error waiting for process to terminate: No child processes
heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
heroku[web.1]: Stopping process with SIGKILL
heroku[web.1]: Process exited with status 22
heroku[web.1]: State changed from starting to crashed
heroku[web.1]: State changed from crashed to starting
my package.json file is below...我的 package.json 文件在下面...
{
"name": "testy",
"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"
}
}
If anyone has any experience in deploying this stack it'd be great to hear from you如果有人在部署此堆栈方面有任何经验,很高兴收到您的来信
Heroku assigns you a port by default and adds the port to the environment variables (env), so you can set the port to a fixed number, you need to change your main file to: Heroku默认为你分配了一个端口,并将端口添加到环境变量(env)中,这样你就可以将端口设置为一个固定的数字,你需要将你的主文件改为:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(process.env.PORT || 3000);
}
bootstrap();
After some hours... it's up and running!几个小时后......它启动并运行!
1. package.json (pay attention on: scripts, @nestjs version, engines) 1. package.json(注意:脚本,@nestjs 版本,引擎)
{
"name": "nest",
"version": "0.0.0",
"description": "description",
"author": "",
"license": "MIT",
"scripts": {
"build": "tsc -p tsconfig.build.json",
"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",
"start:prod": "node dist/main.js",
"prestart:prod": "rimraf dist && npm run build",
"postinstall": "npm run prestart:prod",
"lint": "tslint -p tsconfig.json -c tslint.json",
"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",
"heroku-postbuild": "npm install --only=dev --no-shrinkwrap && npm run build"
},
"dependencies": {
"@nestjs/common": "6.7.2",
"@nestjs/core": "6.7.2",
"@nestjs/jwt": "0.2.0",
"@nestjs/mongoose": "5.2.2",
"@nestjs/passport": "5.1.0",
"@nestjs/platform-express": "6.7.2",
"bcrypt": "3.0.2",
"mongoose": "5.3.15",
"passport": "0.4.0",
"passport-jwt": "4.0.0",
"reflect-metadata": "0.1.12",
"rimraf": "2.6.2",
"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",
"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"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
".+\\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
},
"engines": {
"node": "8.9.4"
}
}
2. Procfile (location - same as package.json) Yes it's file without type. 2. Procfile(位置 - 与 package.json 相同)是的,它是没有类型的文件。 Can be created with Notepad.可以用记事本创建。
web: npm run start:prod
3. Enable installing devDependencies (if needed) 3. 启用安装 devDependencies(如果需要)
heroku config:set NPM_CONFIG_PRODUCTION=false
I made the following changes to deploy a newly generated nest.js app:我进行了以下更改以部署新生成的 nest.js 应用程序:
main.ts
- changed main.ts
- 改变await app.listen(3000);
to到
await app.listen(process.env.PORT || 3000);
added Procfile
contents: web: npm run start:prod
添加Procfile
内容: web: npm run start:prod
package.json
- added heroku-postbuild
script so my scripts look like: package.json
- 添加了heroku-postbuild
脚本,所以我的脚本看起来像:
"scripts": {
"build": "tsc -p tsconfig.build.json",
"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 && npm run build",
"start:prod": "node dist/main.js",
"lint": "tslint -p tsconfig.json -c tslint.json",
"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",
"heroku-postbuild": "npm install --only=dev --no-shrinkwrap && npm run build"
},
Currently running at: https://server-ts.herokuapp.com目前运行于: https : //server-ts.herokuapp.com
If you're using the free tier in Heroku, then you can only use npm start , while yours is start:prod .如果您在 Heroku 中使用免费层,那么您只能使用npm start ,而您的则是start:prod 。 Try replacing "start:prod": "node dist/main.js"
with "start": "node dist/main.js"
.尝试将"start:prod": "node dist/main.js"
替换为"start": "node dist/main.js"
。
Seems that all solutions here was for previous versions of Nest.似乎这里的所有解决方案都适用于以前版本的 Nest。
In my case (NestJS v7.5.1) start command looks like "nest start"
在我的情况下(NestJS v7.5.1)启动命令看起来像"nest start"
And all worked after adding "@nestjs/cli"
to dependencies.在将"@nestjs/cli"
到依赖项后,所有这些都有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.