Registry / testing / babel-plugin-transform-vite-meta-env

babel-plugin-transform-vite-meta-env

JSON →
library1.0.3jsnpmunverified

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.

npm install babel-plugin-transform-vite-meta-env
INSTALL
IMPORT
SIG · BABEL-PLUGIN-TRANS
B
babel-plugin-transform-vite-meta-env
testingjavascriptv1.0.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import plugin from 'babel-plugin-transform-vite-meta-env'
const plugin = require('babel-plugin-transform-vite-meta-env')
Plugin is a default export; works with both ESM and CJS require, but import is preferred for TypeScript.

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.

// babel.config.js module.exports = { plugins: ['babel-plugin-transform-vite-meta-env'], }; // input.js (before) const mode = import.meta.env.MODE; const baseUrl = import.meta.env.BASE_URL; const dev = import.meta.env.DEV; const prod = import.meta.env.PROD; const viteVar = import.meta.env.VITE_VAR; const other = import.meta.env.OTHER; // output.js (after) const mode = process.env.MODE; const baseUrl = '/'; const dev = process.env.NODE_ENV !== 'production'; const prod = process.env.NODE_ENV === 'production'; const viteVar = process.env.VITE_VAR; const other = undefined;
Debug
Known issues
gotchaDoes 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.
fix
Set NODE_ENV explicitly to 'production' or 'development' before running tests.
affects: >=1.0.0
gotchaPlugin only replaces specific keys; unknown keys (e.g., import.meta.env.OTHER) are replaced with undefined, not the full env object.
fix
Define all used env variables in your Vite config and ensure they are prefixed with VITE_, or handle undefined cases in code.
affects: >=1.0.3
deprecatedPart of babel-preset-vite which is in maintenance mode; consider using vitest or jest with vite plugins for better integration.
fix
Migrate tests to Vitest, which natively resolves import.meta.env.
affects: >=1.0.0
deprecatedUse of process.env.NODE_ENV fallback to 'test' may not reflect actual environment.
fix
Set NODE_ENV explicitly in test scripts (e.g., "test": "NODE_ENV=test jest").
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'babel-plugin-transform-vite-meta-env'
Package not installed or incorrectly specified in babel config.
fix
Run 'npm install --save-dev babel-plugin-transform-vite-meta-env' and ensure it's in package.json devDependencies.
import.meta.env is not defined
Plugin not applied or not transforming code (e.g., running in browser without bundler).
fix
Verify Babel configuration includes the plugin and that you are transforming the file (e.g., using @babel/register or build step).
TypeError: Cannot read properties of undefined (reading 'env')
Plugin didn't transform; code is executed in a context where import.meta is undefined (e.g., older Node.js).
fix
Ensure Babel runs on the file and the plugin is active. For Node.js <14, upgrade or use @babel/plugin-syntax-import-meta.
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
20 hits · last 30 days
node
18
OpenAI (training)
1
Resources
babel-plugin-transform-vite-meta-env — npm install babel-plugin-transform-vite-meta-env · libregistry