Skip to content

Build fails if process.env.NODE_ENV is typed by deps #3188

@infomiho

Description

@infomiho

wasp start fails with TS2783: 'NODE_ENV' is specified more than once, so this usage will be overwritten when ProcessEnv interface is augmented with NODE_ENV property.

The explanation

Wasp generates the following code:

export const env = ensureEnvSchema(
  { NODE_ENV: serverDevSchema.shape.NODE_ENV.value, ...process.env },
  serverEnvSchema,
)

This is fine since process.env is usually declared as an object that doesn't contain NODE_ENV e.g. check out @types/node

User reported in Discord that they had next installed which extended the ProcessEnv similar to this:

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

When the ProcessEnv interface includes NODE_ENV spreading process.env creates duplicate NODE_ENV properties which Typescript considers to be a type error.

For the simplest reproduction check out this Typescript playground example

Possible solution

Update Wasp's env.ts generation to use destructuring:

const { NODE_ENV = serverDevSchema.shape.NODE_ENV.value, ...restEnv } = process.env;
export const env = ensureEnvSchema(
  { NODE_ENV, ...restEnv },
  serverEnvSchema,
)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions