3
0

More than 1 year has passed since last update.

NestJSでError: SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a stringと怒られた

Last updated at Posted at 2022-09-09

エラー時の状況

NestJSの環境構築時に起こったエラーの備忘録。

以下の記事を参考にしてNestJSの環境を構築して、yarn start:devした時に怒られた。

Error: SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string

お断り
記事の著者を攻める意図は全くありません。
むしろTypeORM 0.3系の情報なのでめちゃくちゃ感謝しています!

エラー原因の仮説

DBにうまく接続できていないのが原因そう。

  • DBに接続するdatabase.module.tsファイルに不備がある?
  • docker-compose.yamlの内容が間違っている?
    • .envファイルの内容が間違っている?

仮説を検証してみる

一番簡単に対処できそうな設定ファイルをざっと見てみる。

  • docker-compose.yamlの内容が間違っている?
    • .envファイルの内容が間違っている?

タイポはしていなさそう。

.env
POSTGRES_HOST="127.0.0.1"
POSTGRES_PORT="5432"
POSTGRES_DB="postgres"
POSTGRES_USER="root"
POSTGRES_PASSWORD="password"
docker-compose.yaml
version: '3'

services:
  db:
    image: postgres
    volumes:
      - ./db/data:/var/lib/postgresql/data
      - ./db/postgres/initdb:/docker-entrypoint-initdb.d
    ports:
      - '${POSTGRES_PORT}:5432'
    environment:
      - TZ=Asia/Tokyo
      - POSTGRES_DB=${POSTGRES_DB}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}

DBに接続するdatabase.module.tsファイルに不備がある?

タイポはなさそうなのでdatabase.module.tsの内容を見てみる。
うむ、記事と比較しても内容的に問題なさそう???

いや、もしかするとここで.envファイルの設定を何かしらする必要がある?

database.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => {
        return {
          type: 'postgres',
          host: configService.get('POSTGRES_HOST'),
          port: configService.get('POSTGRES_PORT'),
          database: configService.get('POSTGRES_DB'),
          username: configService.get('POSTGRES_USER'),
          password: configService.get('POSTGRES_PASSWORD'),
          entities: [Todo],
          synchronize: true,
        };
      },
    }),
  ],
})
export class DatabaseModule {}

.envファイルの設定が必要なのかググってみる

するとapp.module.tsに環境変数の設定をしている記事を見つけた。

今回は、database.module.tsに環境変数の設定を追加しても問題なさそうか試してみる。

database.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { Todo } from 'src/todo/entities/todo.entity';

@Module({
  imports: [
+    TypeOrmModule.forRootAsync({
+      imports: [
+        ConfigModule.forRoot({
+          envFilePath: ['.env'],
+        }),
+      ],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => {
        return {
          type: 'postgres',
          host: configService.get('POSTGRES_HOST'),
          port: configService.get('POSTGRES_PORT'),
          database: configService.get('POSTGRES_DB'),
          username: configService.get('POSTGRES_USER'),
          password: configService.get('POSTGRES_PASSWORD'),
          entities: [Todo],
          synchronize: true,
        };
      },
    }),
  ],
})
export class DatabaseModule {}

これでログを見てみると、解消されていました!

🎉🎉🎉

原因

.envファイルの読み込みの設定が、app.module.tsに紐付いていないのが原因でした。

Source and Thanks

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0