Skip to content

Environment Variables

Dotenv Files

WXT supports dotenv files the same way as Vite. Create any of the following files:

.env
.env.local
.env.[mode]
.env.[mode].local
.env.[browser]
.env.[browser].local
.env.[mode].[browser]
.env.[mode].[browser].local

And any environment variables listed inside them will be available at runtime:

sh
# .env
WXT_API_KEY=...
ts
await fetch(`/some-api?apiKey=${import.meta.env.WXT_API_KEY}`);

Remember to prefix any environment variables with WXT_ or VITE_, otherwise they won't be available at runtime, as per Vite's convention.

Built-in Environment Variables

WXT provides some custom environment variables based on the current command:

UsageTypeDescription
import.meta.env.MANIFEST_VERSION2 │ 3The target manifest version
import.meta.env.BROWSERstringThe target browser
import.meta.env.CHROMEbooleanEquivalent to import.meta.env.BROWSER === "chrome"
import.meta.env.FIREFOXbooleanEquivalent to import.meta.env.BROWSER === "firefox"
import.meta.env.SAFARIbooleanEquivalent to import.meta.env.BROWSER === "safari"
import.meta.env.EDGEbooleanEquivalent to import.meta.env.BROWSER === "edge"
import.meta.env.OPERAbooleanEquivalent to import.meta.env.BROWSER === "opera"

You can also access all of Vite's environment variables:

UsageTypeDescription
import.meta.env.MODEstringThe mode the extension is running in
import.meta.env.PRODbooleanWhen NODE_ENV='production'
import.meta.env.DEVbooleanOpposite of import.meta.env.PROD
Other Vite Environment Variables

Vite provides two other environment variables, but they aren't useful in WXT projects:

  • import.meta.env.BASE_URL: Use browser.runtime.getURL instead.
  • import.meta.env.SSR: Always false.

Manifest

To use environment variables in the manifest, you need to use the function syntax:

ts
export default defineConfig({
  extensionApi: 'chrome',
  modules: ['@wxt-dev/module-vue'],
  manifest: { 
    oauth2: { 
      client_id: import.meta.env.WXT_APP_CLIENT_ID
    } 
  } 
  manifest: () => ({ 
    oauth2: { 
      client_id: import.meta.env.WXT_APP_CLIENT_ID
    } 
  }), 
});

WXT can't load your .env files until after the config file has been loaded. So by using the function syntax for manifest, it defers creating the object until after the .env files are loaded into the process.