1
0

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 1 year has passed since last update.

Viteで環境変数を使用する

Posted at

Viteで.envファイルに入れた環境変数を使用するのに少し手間取ったので備忘録としてメモをしておきます。

環境
vite@3.1.8
vue@3.2.41

vueのやり方で試してみる

以下のやり方で試してみた

.env
TESTKEY=1234
APP.vue
console.log(process.env.TESTKEY)

これをするとエラーになった。どうやら通常のvueのやり方は使えないらしい

マニュアルを読んでみた

Viteの公式マニュアルを読むと以下のようなやり方でできた。
(初めから読めよという話、、、)

・公式マニュアル
https://vitejs.dev/guide/env-and-mode.html#env-variables-and-modes

APP.vue
console.log(import.meta.env.TESTKEY) 

viteで立ち上げるとvueのコマンドが使えなくなることがるみたいです。
(コンパイルの方法が違う←全然違っていたらすいません)
vue初心者の私にはそこらへんがわかっておらず混乱しました。
※ちなみに@vueuse/headも使えませんでした

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?