LoginSignup
1
0
記事投稿キャンペーン 「2024年!初アウトプットをしよう」

bcryptを使ってpasswordをハッシュ化させる際に起きた、Cannot read properties of undefined (reading 'hashSync') を解決

Posted at

はじめに

NestJSを使用したバックエンドの中で、bcryptを使ってパスワードをハッシュ化するために

const hashedPassword = bcrypt.hashSync(user.password, saltRounds);

とコードを書くと、ユーザー登録時に

 Cannot read properties of undefined (reading 'hashSync')

というエラーが発生しました。

解決方法

基本的にbcryptの使い方は こちら を参考に確認しており、bcryptのインポート方法も

import bcrypt from "bcrypt";

と記述していましたが、これを

import * as bcrypt from 'bcrypt';

と書き換えることでエラーは解決されました。

詳しくはこちらのStackoverflowでの回答が参考になると思います。

おわりに

同じエラーに遭われた方のお役に立てられれば光栄です。

1
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
1
0