Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nicolaspadari/vite-plugin-envloader

Automatically inject import.meta.env variables into process.env
https://github.com/nicolaspadari/vite-plugin-envloader

Last synced: about 1 month ago
JSON representation

Automatically inject import.meta.env variables into process.env

Awesome Lists containing this project

README

        

vite-plugin-envloader

Automatically inject `import.meta.env` variables into `process.env`





## Why?

Vite.js provides its own mechanism (meta env) for exposing environment variables through `import.meta.env`, but hostings like Netlify can't import variables defined in `import.meta.env`. [Here is the issue](https://github.com/vitejs/vite/issues/562)

This plugin injects environment variables in `process.env` by configuring the `define` option.

## Installation

Install the package as a development dependency:

```sh
npm i -D vite-plugin-envloader
```

## Usage

Add this to your plugin list in the vite config file:

```js
import { defineConfig } from "vite"
import EnvLoader from "vite-plugin-envloader"

export default defineConfig({
plugins: [
// ...
EnvLoader(),
// ...
],
});
```

Variables defined in the `.env` file like:

```js
VITE_API_KEY="12345"
```

Will be available in online hostings through `process.env.VITE_API_KEY`, while still allowing you to use `import.meta.env.VITE_API_KEY` in your code.

## License

This library is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).