Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 24, 2025

Fixes build failures when external TypeScript dependencies (like Next.js) extend the ProcessEnv interface with NODE_ENV property.

Problem

When external libraries augment the ProcessEnv interface with NODE_ENV, Wasp's server env generation fails with TypeScript error TS2783: 'NODE_ENV' is specified more than once, so this usage will be overwritten. This occurs because the original code creates duplicate property keys:

// Problematic approach that causes TS2783
export const env = ensureEnvSchema(
  { NODE_ENV: serverDevSchema.shape.NODE_ENV.value, ...process.env },
  serverEnvSchema,
)

The issue manifests when users have dependencies like Next.js that declare:

declare namespace NodeJS {
  interface ProcessEnv {
    NODE_ENV: "development" | "production";
  }
}

Solution

Use JavaScript destructuring to avoid duplicate property keys:

// Fixed approach using destructuring
const { NODE_ENV = serverDevSchema.shape.NODE_ENV.value, ...restEnv } = process.env;
export const env = ensureEnvSchema(
  { NODE_ENV, ...restEnv },
  serverEnvSchema,
)

This approach:

  • Extracts NODE_ENV from process.env with a default value
  • Spreads the remaining environment variables separately
  • Avoids TypeScript's duplicate property key detection
  • Maintains identical runtime behavior

Changes

  • Updated server env template in waspc/data/Generator/templates/sdk/wasp/server/env.ts
  • Updated all e2e test golden files to match new expected output
  • Client env template requires no changes (uses import.meta.env directly)

Note

Coded by Copilot, reviewed by human.

Copy link

Deploying wasp-docs-on-main with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0948884
Status: ✅  Deploy successful!
Preview URL: https://f400571b.wasp-docs-on-main.pages.dev
Branch Preview URL: https://copilot-fix-3584091d-5ae9-47.wasp-docs-on-main.pages.dev

View logs

@Copilot Copilot AI changed the title [WIP] Build fails if process.env.NODE_ENV is typed by deps Fix NODE_ENV TypeScript duplication error in server env template Sep 24, 2025
@Copilot Copilot AI requested a review from infomiho September 24, 2025 15:43
Copilot finished work on behalf of infomiho September 24, 2025 15:43
@infomiho
Copy link
Contributor

@copilot you didn't do the equivalent change in the client code

Copy link
Contributor Author

Copilot AI commented Sep 24, 2025

@copilot you didn't do the equivalent change in the client code

Could you clarify what specific client-side change is needed? I looked at the client env template and it currently uses ensureEnvSchema(import.meta.env, clientEnvSchema) directly without the duplicate property pattern that caused the server-side issue.

The server issue was from { NODE_ENV: defaultValue, ...process.env } but the client side doesn't seem to have this pattern. Are you referring to a specific file or scenario where the client code needs the same destructuring approach?

Copilot finished work on behalf of infomiho September 24, 2025 16:05
@infomiho infomiho marked this pull request as ready for review September 25, 2025 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants