https://github.com/tommywalkie/vite-register
Use Vite env variables inside Node scripts and test runners via a require hook.
https://github.com/tommywalkie/vite-register
dotenv import-meta mocha require-hook test uvu vite
Last synced: 3 months ago
JSON representation
Use Vite env variables inside Node scripts and test runners via a require hook.
- Host: GitHub
- URL: https://github.com/tommywalkie/vite-register
- Owner: tommywalkie
- License: mit
- Created: 2021-10-28T07:42:36.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-01-13T14:14:28.000Z (over 4 years ago)
- Last Synced: 2025-10-06T22:40:26.774Z (8 months ago)
- Topics: dotenv, import-meta, mocha, require-hook, test, uvu, vite
- Language: TypeScript
- Homepage:
- Size: 27.3 KB
- Stars: 2
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
vite-register
## Install
```bash
npm install --save-dev vite-register
```
## Features
Pretty much [like Vite does](https://vitejs.dev/guide/env-and-mode.html#production-replacement), vite-register will statically replace variables.
- [x] Support `import.meta.env.MODE`
- [x] Support `import.meta.env.DEV`
- [x] Support `import.meta.env.PROD`
- [ ] Support `import.meta.env.SSR` (defaults to `false`)
- [x] Support `import.meta.env.BASE_URL`
- [x] Support loading variables
- [x] from a `.env` file
- [x] from a `.env.local` file
- [x] from a `.env.[mode]` file
- [x] from a `.env.[mode].local` file
- [x] Support `.envPrefix` (defaults to `VITE_`)
- [x] Support `.envDir` (defaults to project root)
## Usage
This hook can be used with the Node CLI and some test runners supporting hooks via the `-r` (`--require`) option.
```bash
# Assuming envPrefix => 'VITE_'
echo 'VITE_FOO="hello world"' > .env
echo 'console.log(import.meta.env.VITE_FOO);' > index.js
node -r vite-register index.js
> hello world
```
vite-register can look into Vite configuration files (`vite.config.[ext]`) and retrieve [`base`](https://vitejs.dev/config/#base) and [`mode`](https://vitejs.dev/config/#mode).
In most cases, TypeScript/JSX/ESM support in configuration files and scripts can be provided by third-party hooks like [tsm](https://github.com/lukeed/tsm), [esbuild-runner](https://github.com/folke/esbuild-runner) or [esbuild-register](https://github.com/egoist/esbuild-register), before vite-register hook.
```bash
node -r esbuild-runner/register -r vite-register index.ts
```
### Mocha
Can be combined with [esbuild-runner](https://github.com/folke/esbuild-runner) for TypeScript/JSX/ESM support.
```bash
mocha -r esbuild-runner/register -r vite-register [pattern]
```
### uvu
↪️ lukeed/uvu
Can be combined with [tsm](https://github.com/lukeed/tsm) or [esbuild-runner](https://github.com/folke/esbuild-runner) for TypeScript/JSX/ESM support.
```bash
uvu -r tsm -r vite-register [pattern]
```
## FAQ
### When should I use vite-register over [Vitest](https://github.com/vitest-dev/vitest), or vice-versa ?
- If you want a test framework, **use Vitest**.
- If you need Jest features (snapshot, mocking, etc.), **use Vitest**.
- If package size [really matters](https://packagephobia.com/result?p=vitest%2C+vite-register), **use vite-register**.
- If you want something actively maintained by Vite members, **use Vitest**.
- If migrating tests becomes an issue, **use vite-register**.
- If you _really_ can't upgrade to Node >=v14, **use vite-register**.
### Support Jest ?
Jest currently doesn't support hooks ([facebook/jest#11295](https://github.com/facebook/jest/issues/11295)). If you need Jest unique features, you're better off switching to [Vitest](https://github.com/vitest-dev/vitest).
## License
MIT