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

@fantasticfour/dotenvx-next

v0.2.4

Published

Next.js plugin that loads dotenvx secrets early — before any user code evaluates — by injecting an init bundle into webpack/turbopack runtime files at build time.

Downloads

2,000

Readme

@fantasticfour/dotenvx-next

dotenvx-next

Decrypt dotenvx secrets before any Next.js module initializes.

A Next.js plugin that loads dotenvx secrets before any user code evaluates at runtime. It solves the timing problem where encrypted env vars need to be decrypted before modules like Prisma initialize their connection pools.

The problem

dotenvx decrypts secrets at startup, but Next.js evaluates server modules before that decryption runs. Two common patterns break silently:

instrumentation.ts — Next.js's official hook for startup side-effects runs after modules have already been imported. By the time dotenvx.config() executes there, any module-scope code has already read process.env:

// instrumentation.ts — too late
export async function register() {
  const { config } = await import('@dotenvx/dotenvx');
  config(); // DATABASE_URL was already read as undefined
}

Module-scope process.env access — Anything that reads an env var at the top level of a module (outside a function) runs at import time, before any instrumentation or config call:

// db.ts — evaluated at import time
const pool = new Pool({ connectionString: process.env.DATABASE_URL }); // undefined ✗

This plugin decrypts env files at build time and inlines the resolved values directly into the webpack and turbopack runtime, so they are available before any user module is imported.

Install

npm install @fantasticfour/dotenvx-next

Requires next >= 14.

Usage

Wrap your Next.js config with withDotenvx:

// next.config.ts
import { withDotenvx } from '@fantasticfour/dotenvx-next';

export default withDotenvx({
  // your existing Next.js config
});

By default it loads .env from your project root. Because values are inlined at build time, no encrypted env files need to be present in the deployed serverless bundle, and you do not need dotenvx run in your dev or build scripts.

Full @next/env coverage with package overrides

The plugin calls dotenvx.config() at build time (during next.config.ts evaluation) and inlines the resolved values into the webpack/turbopack runtime. This covers the common case — module-scope process.env reads at import time.

However, Next.js also calls loadEnvConfig from @next/env during its own Node.js startup, before webpack runs. The webpack resolve.alias set by this plugin only intercepts imports inside webpack-compiled code, so those startup calls are not covered by the alias alone.

To get full coverage at the Node.js package-resolution level, add an override so that require('@next/env') resolves to this package instead:

npm (package.json):

{
  "overrides": {
    "@next/env": "@fantasticfour/dotenvx-next"
  }
}

pnpm (package.json):

{
  "pnpm": {
    "overrides": {
      "@next/env": "@fantasticfour/dotenvx-next"
    }
  }
}

Yarn (package.json):

{
  "resolutions": {
    "@next/env": "@fantasticfour/dotenvx-next"
  }
}

With this override, require('@next/env').loadEnvConfig returns the wrapped version that calls dotenvx.config() first — at all call sites, including Next.js's own startup code.

Multiple environments

The files option accepts any list of env files. Because the plugin resolves them at build time, you are responsible for selecting the right files for the current environment. On Vercel, VERCEL_ENV is set during the build:

// next.config.ts
const vercelEnv = process.env.VERCEL_ENV;

export default withDotenvx(config, {
  files:
    vercelEnv === 'production'
      ? ['.env.base', '.env.production']
      : vercelEnv === 'preview'
        ? ['.env.base', '.env.preview']
        : ['.env'], // local dev
});

Passing an empty array (files: []) skips decryption entirely for that build, which is useful if you want to handle local dev with dotenvx run separately rather than baking values into the dev server runtime.

Options

withDotenvx(nextConfig, {
  // Env files to load. Defaults to ['.env']
  files: ['.env', '.env.production'],

  // Directory where env files live. Defaults to process.cwd()
  envDir: '/path/to/env/files',
})

With an async config function

export default withDotenvx(async (phase) => {
  return {
    // your config
  };
});

How it works

At build time, dotenvx.config() decrypts the specified env files and the resolved key-value pairs are serialized into a compact Object.assign(process.env, {...}) snippet.

Webpack builds: DotenvxWebpackPlugin prepends this snippet to webpack-runtime.js via the processAssets hook at PROCESS_ASSETS_STAGE_ADDITIONS, so every value is available before any module code runs.

Turbopack builds: Since turbopack bypasses webpack's plugin API, the plugin patches fs.writeFile to detect when compilation completes (signaled by export-detail.json being written), then prepends the same snippet into [turbopack]_runtime.js.

@next/env webpack alias: The plugin sets webpack resolve.alias['@next/env'] to a compat shim that wraps loadEnvConfig to run dotenvx.config() first. This covers any import of @next/env inside webpack-compiled server bundles (hot-reload handlers, preview mode helpers). It does not cover Next.js's own pre-webpack startup calls — use the npm overrides pattern for full coverage.

Edge runtime: The webpack plugin detects edge runtime compilations via compiler.options.target and injects into edge-runtime-webpack.js rather than webpack-runtime.js. The inline snippet uses a typeof process !== 'undefined' guard, so it is safe in edge runtimes where process may be absent.

Debugging

Set DEBUG_DOTENVX_NEXT=1 to see injection logs during your build.