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?

【Vite】npm run buildするとReferenceError: env is not definedエラーが出る

Posted at

はじめに

process.envで環境変数を読み込めるようにvite.config.jsを修正したところエラーになったので、戒めをこめて記事にします。

問題

process.envで環境変数を読み込めるようにvite.config.jsを修正したところReferenceError: env is not definedエラーとなりビルドできない。

vite.config.js
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

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

解決方法

vite-plugin-env-compatibleをimportできていなかった。

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

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

ライブラリのインストールもできていなかったため、インストール

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

おわりに

importを忘れるという、ちょっと恥ずかしいエラーでした。

参考

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?