{"id":19225,"library":"babel-plugin-transform-vite-meta-env","title":"babel-plugin-transform-vite-meta-env","description":"Babel plugin that emulates Vite's import.meta.env functionality for non-Vite environments, such as testing with NodeJS-based runners. Version 1.0.3 is the latest stable release, last updated in April 2021. It transforms import.meta.env.MODE, .BASE_URL, .DEV, .PROD, etc. into process.env references or static values, providing an approximation of Vite's env behavior. Differentiated from alternatives like @rollup/plugin-replace by specifically targeting Vite idioms and supporting a set of predefined env variables. Release cadence is low; part of the babel-vite monorepo. Ships TypeScript types.","status":"maintenance","version":"1.0.3","language":"javascript","source_language":"en","source_url":"https://github.com/OpenSourceRaidGuild/babel-vite","tags":["javascript","babel","vite","plugin","import","meta","env","typescript"],"install":[{"cmd":"npm install babel-plugin-transform-vite-meta-env","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-transform-vite-meta-env","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-transform-vite-meta-env","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Plugin is a default export; works with both ESM and CJS require, but import is preferred for TypeScript.","wrong":"const plugin = require('babel-plugin-transform-vite-meta-env')","symbol":"default","correct":"import plugin from 'babel-plugin-transform-vite-meta-env'"}],"quickstart":{"code":"// babel.config.js\nmodule.exports = {\n  plugins: ['babel-plugin-transform-vite-meta-env'],\n};\n\n// input.js (before)\nconst mode = import.meta.env.MODE;\nconst baseUrl = import.meta.env.BASE_URL;\nconst dev = import.meta.env.DEV;\nconst prod = import.meta.env.PROD;\nconst viteVar = import.meta.env.VITE_VAR;\nconst other = import.meta.env.OTHER;\n\n// output.js (after)\nconst mode = process.env.MODE;\nconst baseUrl = '/';\nconst dev = process.env.NODE_ENV !== 'production';\nconst prod = process.env.NODE_ENV === 'production';\nconst viteVar = process.env.VITE_VAR;\nconst other = undefined;","lang":"javascript","description":"Configures Babel to replace import.meta.env references with process.env equivalents or static values, useful for testing Vite-dependent code in non-Vite environments."},"warnings":[{"fix":"Set NODE_ENV explicitly to 'production' or 'development' before running tests.","message":"Does not handle import.meta.env.SSR or import.meta.env.PROD correctly when NODE_ENV is not set; DEV may be truthy in test environments.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Define all used env variables in your Vite config and ensure they are prefixed with VITE_, or handle undefined cases in code.","message":"Plugin only replaces specific keys; unknown keys (e.g., import.meta.env.OTHER) are replaced with undefined, not the full env object.","severity":"gotcha","affected_versions":">=1.0.3"},{"fix":"Migrate tests to Vitest, which natively resolves import.meta.env.","message":"Part of babel-preset-vite which is in maintenance mode; consider using vitest or jest with vite plugins for better integration.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Set NODE_ENV explicitly in test scripts (e.g., \"test\": \"NODE_ENV=test jest\").","message":"Use of process.env.NODE_ENV fallback to 'test' may not reflect actual environment.","severity":"deprecated","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run 'npm install --save-dev babel-plugin-transform-vite-meta-env' and ensure it's in package.json devDependencies.","cause":"Package not installed or incorrectly specified in babel config.","error":"Cannot find module 'babel-plugin-transform-vite-meta-env'"},{"fix":"Verify Babel configuration includes the plugin and that you are transforming the file (e.g., using @babel/register or build step).","cause":"Plugin not applied or not transforming code (e.g., running in browser without bundler).","error":"import.meta.env is not defined"},{"fix":"Ensure Babel runs on the file and the plugin is active. For Node.js <14, upgrade or use @babel/plugin-syntax-import-meta.","cause":"Plugin didn't transform; code is executed in a context where import.meta is undefined (e.g., older Node.js).","error":"TypeError: Cannot read properties of undefined (reading 'env')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}