npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

vite-plugin-typenv

v0.3.0

Published

A Vite plugin to use JS/TS files for environment variables instead of .env files

Readme

vite-plugin-typenv

Use JS/TS files for environment variables over .env files, and provides complete TypeScript type support.

Usage

Install the plugin:

npm install -D vite-plugin-typenv

Add it to your Vite config:

// vite.config.js
import { defineConfig } from 'vite'
import typenv from 'vite-plugin-typenv'

export default defineConfig({
  plugins: [
    typenv(),
  ],
})

The plugin will load additional environment variables from the following files in your environment directory:

env.js              # loaded in all cases
env.local.js        # loaded in all cases, ignored by git
env.[mode].js       # only loaded in specified mode
env.[mode].local.js # only loaded in specified mode, ignored by git

To get TypeScript IntelliSense, create an vite-env.d.ts in src directory, and augment UserDefinedEnvVariables (instead of ImportMetaEnv, see Intellisense for TypeScript for more details):

// src/vite-env.d.ts
interface UserDefinedEnvVariables {
  /**
   * App title
   */
  readonly VITE_APP_TITLE: string
  // more env variables...
}

Then add vite/client and vite-plugin-typenv/client to your tsconfig.json:

// tsconfig.json
{
  "compilerOptions": {
    "types": ["vite/client", "vite-plugin-typenv/client"]
  }
}

Alternatively, you can add the following directive to your vite-env.d.ts file:

// src/vite-env.d.ts
/// <reference types="vite/client" />
/// <reference types="vite-plugin-typenv/client" />

[!NOTE] After doing this, import.meta.env will also be typed.

Replace your original .env files with env.js or env.ts files, and use the defineDefaultVariables function to define env variables:

// env.js
import { defineDefaultVariables } from 'vite-plugin-typenv'

export default defineDefaultVariables({
  VITE_APP_TITLE: 'Vite App', // Now fully type-safe with IntelliSense
})

Local env files and mode-specific env files are also supported, use the defineVariables function to define env variables:

// env.local.js
import { defineVariables } from 'vite-plugin-typenv'

export default defineVariables({
  VITE_APP_TITLE: 'Vite App Local', // All env variables here are optional
})

[!NOTE] By default, only variables prefixed with VITE_ are exposed to your Vite-processed code. You can customize this behavior by setting the envPrefix option.

Also, variables expansion is supported and enabled by default. The core logic is directly copied from dotenv-expand, refer to their docs to learn more about the syntax.

Note that if you want to use $ in your env variables, you need to escape it with \.

// env.local.js
import { defineVariables } from 'vite-plugin-typenv'

export default defineVariables({
  VITE_USERNAME: 'keroz',
  VITE_MESSAGE: 'Hello $VITE_USERNAME', // Hello keroz
  VITE_ESCAPED_MESSAGE: '\\$foo', // $foo
})

[!NOTE]

  • env.*.local.js/ts files are local-only and can contain sensitive variables. You should add *.local.js/ts to your .gitignore to avoid them being checked into git.
  • Since any variables exposed to your Vite source code will end up in your client bundle, VITE_* variables should not contain any sensitive information.

To use environment variables in Vite config file, you can use loadEnv helper function:

// vite.config.js
import { defineConfig } from 'vite'
import { loadEnv } from 'vite-plugin-typenv'

export default defineConfig(async ({ mode }) => {
  const env = await loadEnv(mode)

  return {
    base: env.VITE_BASE_URL
  }
})

See also Using Environment Variables in Config in Vite docs.