I configured TypeORM for Heroku using a guide and the ormconfig.js
looks like this:
const dbConfig = {
synchronize: false,
migrations: ['migrations/*.js'],
cli: {
migrationsDir: 'migrations',
},
};
switch(process.env.NODE_ENV){
case 'development':
Object.assign(dbConfig, {
type: 'sqlite',
database: 'db.sqlite',
entities: ['**/*.entity.js'],
});
break;
case 'test':
Object.assign(dbConfig, {
type: 'sqlite',
database: 'test.sqlite',
entities: ['**/*.entity.ts'],
migrationsRun: true,
ssl: {
rejectUnauthorized: false
}
});
break;
case 'production':
Object.assign(dbConfig, {
type: 'postgres',
url: process.env.DATABASE_URL,
migrationsRun: true,
entities: ['**/*.entity.js'],
});
break;
default:
throw new Error('unknown environment')
}
module.exports = dbConfig;
I have also created a Procfile
file in the project for Heroku with the command web: npm run start:prod
.
I only have a few simple routes at the moment and locally I can run the api with SQLite without errors. But when I try to run the api on Heroku, I get this error:
2022-07-17T11:53:53.748981+00:00 app[web.1]: error: no pg_hba.conf entry for host "xx.xxx.xxx.xxx", user "foofoofoof", database "foofoofoof", no encryption
2022-07-17T11:53:53.748981+00:00 app[web.1]: at Parser.parseErrorMessage (/app/node_modules/pg-protocol/dist/parser.js:287:98)
2022-07-17T11:53:53.748982+00:00 app[web.1]: at Parser.handlePacket (/app/node_modules/pg-protocol/dist/parser.js:126:29)
2022-07-17T11:53:53.748982+00:00 app[web.1]: at Parser.parse (/app/node_modules/pg-protocol/dist/parser.js:39:38)
2022-07-17T11:53:53.748983+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/pg-protocol/dist/index.js:11:42)
2022-07-17T11:53:53.748983+00:00 app[web.1]: at Socket.emit (node:events:527:28)
2022-07-17T11:53:53.748984+00:00 app[web.1]: at addChunk (node:internal/streams/readable:315:12)
2022-07-17T11:53:53.748984+00:00 app[web.1]: at readableAddChunk (node:internal/streams/readable:289:9)
2022-07-17T11:53:53.748984+00:00 app[web.1]: at Socket.Readable.push (node:internal/streams/readable:228:10)
2022-07-17T11:53:53.748984+00:00 app[web.1]: at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
2022-07-17T11:53:53.879579+00:00 heroku[web.1]: Process exited with status 1
2022-07-17T11:53:53.945790+00:00 heroku[web.1]: State changed from starting to crashed
I configured my app.module
using the same instructions for my database and it looks like this:
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { IngredientsModule } from './ingredients/ingredients.module';
import { RecipesModule } from './recipes/recipes.module';
import { UsersModule } from './users/users.module';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: `.env.${process.env.NODE_ENV}`,
}),
TypeOrmModule.forRoot(),
UsersModule,
IngredientsModule,
RecipesModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
For Heroku, I created three environment variables COOKIE_KEY
, DATABASE_URL
(was created automatically), NODE_ENV
under Config Vars:
My script section in package.json
looks like this:
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "cross-env NODE_ENV=development nest start",
"start:dev": "cross-env NODE_ENV=development nest start --watch",
"start:debug": "cross-env NODE_ENV=development nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "cross-env NODE_ENV=test jest",
"test:watch": "cross-env NODE_ENV=test jest --watch --maxWorkers=1",
"test:cov": "cross-env NODE_ENV=test jest --coverage",
"test:debug": "cross-env NODE_ENV=test node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "cross-env NODE_ENV=test jest --config ./test/jest-e2e.json --maxWorkers=1",
"typeorm": "cross-env NODE_ENV=development node --require ts-node/register ./node_modules/typeorm/cli.js"
},
How can I fix this error in NestJS or Heroku?
I found a solution how to get rid of this error.
Run in the console:
heroku config:set PGSSLMODE=no-verify