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 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-config-env-variables-patch

v1.0.3

Published

This package is a patch for nextjs standalone build. It patches server.js to be able to use dynamic ENV variabled for the publicRuntimeConfig and serverRuntimeConfig

Downloads

11

Readme

next-config-env-variables-patch

Overview

next-config-env-variables-patch is an npm package designed to address a common challenge faced when building NextJS projects in production mode with the output="standalone" configuration. Typically, NextJS inlines environment variables (like process.env.SOME_ENV) during the build time, making it impossible to modify these values post-build. This limitation is particularly problematic when deploying the same Docker image across different stages (QA, staging, production, etc.) and can lead to increased build times and image sizes.

The Problem

In NextJS, publicRuntimeConfig and serverRuntimeConfig can be defined in next.config.js for dynamic runtime configuration. However, this only works with output="export". In output="standalone" mode, next.config.js is embedded in the server.js file, and all environment variables are fixed at build time. This rigid behavior breaks the "Build once, deploy many" rule, especially in Dockerized environments.

Installation

Install the package using Yarn:

yarn add next-config-env-variables-patch -D

Usage

  1. Modify your next.config.js as follows:

    module.exports = {
      output: "export",
      publicRuntimeConfig: {
        ENV_PUBLIC_API_BASE_URL: process.env.ENV_PUBLIC_API_BASE_URL,
      },
      serverRuntimeConfig: {
        ENV_SOME_ENV_VARIABLE: process.env.ENV_SOME_ENV_VARIABLE,
      },
    }

    Ensure to prefix the properties with ENV_.

  2. Update the build script in package.json:

    "build": "next build && next-config-env-variables-patch"
  3. Build your application with yarn build using the standalone output.

  4. Run your Dockerized NextJS application, setting the environment variables as needed:

    docker run -p 3000:3000 -e ENV_PUBLIC_API_BASE_URL=http://host.docker.internal:8080/api my-nextjs-application:latest

How It Works

next-config-env-variables-patch modifies the server.js. It adds an additional code to the server.js before starting the server

// DYNAMIC_CONFIG_ENV_VARIABLES_PATCH_START
if (nextConfig.publicRuntimeConfig) {
  for (const key in nextConfig.publicRuntimeConfig) {
    if (nextConfig.publicRuntimeConfig.hasOwnProperty(key) && key.startsWith('ENV_')) {
      nextConfig.publicRuntimeConfig[key] = process.env[key] || nextConfig.publicRuntimeConfig[key];   
    }
  }
}
if (nextConfig.serverRuntimeConfig) {
  for (const key in nextConfig.serverRuntimeConfig) {
    if (nextConfig.serverRuntimeConfig.hasOwnProperty(key) && key.startsWith('ENV_')) {
      nextConfig.serverRuntimeConfig[key] = process.env[key] || nextConfig.serverRuntimeConfig[key];   
    }
  }
}
process.env.__NEXT_PRIVATE_STANDALONE_CONFIG = JSON.stringify(nextConfig);
// DYNAMIC_CONFIG_ENV_VARIABLES_PATCH_END

This code gets publicRuntimeConfig and serverRuntimeConfig that was generated at the build time and replaces all the properties in configs that start with ENV_ prefix with the environment variable with the same name. If there is no environment variable with same name, the default generated value is used.