8
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have 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
})
8
1
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
8
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?