JavaScript/TypeScript

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

kohi ☕ 2023. 3. 26. 03:10

Problem

타입스크립트 환경에서 env 변수 사용 시 import.meta.env에서 타입 오류 발생

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

 

Reference

https://github.com/vitejs/vite/issues/9539

 

Property 'env' does not exist on type 'ImportMeta' in 3.0.0 · Issue #9539 · vitejs/vite

Describe the bug In vite 3.0.0 it still exists, even if it was previously closed. Can be recreated using react-ts template. Try to use env variable via import.meta.env, TS will give you an error Pr...

github.com

 

Try to solve

  • .env.d.ts 파일 생성 후 타입 정의
interface ImportMeta {
    env: {
        VITE_REST_API_KEY: string;
    };
}

 

  • tsconfig 추가
"types": ["vite/client"]