8
2

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でprocess.envを使う方法

Last updated at Posted at 2024-02-18

はじめに

Viteで環境変数を使うのにimport.metaを使わずにprocess.envを使いたかったのでまとめます

やり方

vite.config.tsに以下を追加します

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
+ import env from "vite-plugin-env-compatible";

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
    react(),
+   env({ prefix: "VITE",  mountedPath: "process.env" }) 
  ],
});

そして必要なライブラリをインストールします

$ npm i --save-dev vite-plugin-env-compatible

これで通常通りprocess.envが利用できるようになります

もしdotenvの読み込みの設定を行なっていない場合はそれも併せてやりましょう

おわりに

import.meta...を使うのが微妙だったので解決できてよかったです

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?