2
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?

Property 'env' does not exist on type 'ImportMeta'. any

2
Posted at

はじめに

Vite x TypeScript/ Reactでの環境変数使用時にハマったのでメモ。

問題

Supabaseクライアント使用時、APIキーなどが必要なときに環境変数を利用する場面があると思います。

supabase.ts
import { createClient } from "@supabase/supabase-js"

export const supabase = createClient(
  import.meta.env.VITE_SUPABASE_URL,
  import.meta.env.VITE_SUPABASE_PUBLISHABLE_KEY,
)

すると、envあたりに以下のようなエラーが。

Property 'env' does not exist on type 'ImportMeta'.
any

直訳すると、

プロパティ 'env' は型 'ImportMeta' に存在しません。

うーんよくわからぬ、、、

解決方法

なぜこれが起きたかはわからないが、tsconfig.jsonのcompilerOptions.typesにvite/clientを追加してあげればなおりました。

tsconfig.json
{
  "compilerOptions": {
    "types": ["node", "jest", "@testing-library/jest-dom", "vite/client"]
  },
}

おわりに

調べると、これはVSCode のTypeScript言語サポートが原因っぽい。

2
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
2
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?