LoginSignup
6
0

More than 1 year has passed since last update.

typescriptでdoenvを使うときのメモ

Posted at

dotenvを使って環境変数を使えるようにする

ライブラリーをインストール

npm install dotenv

.env ファイルを作り環境変数の値を設定する

#DB_HOST="localhost"  #はコメントアウト

SESSION_SECRET="secret"
TOKEN_SECRET="secret"

ルートファイル( 今回はindex.ts )でインポートする

// jsならしたのように
require('dotenv').config()

// tsならしたのように書けば使える
import 'dotenv/config'

// 使用例
const * as db = import 'db'
db.connect({
  host: process.env.DB_HOST,
  username: process.env.DB_USER,
  password: process.env.DB_PASS
})
6
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
6
0