W
Waspβ€’3w ago
aspiringaiguy

`NODE_ENV` Error during build

I get the error below during local and production builds. No clue as to why:
🐝 --- Building SDK... ------------------------------------------------------------


[ Wasp !] (node:43832) ExperimentalWarning: CommonJS module /opt/homebrew/lib/node_modules/npm/node_modules/debug/src/node.js is loading ES Module /opt/homebrew/lib/node_modules/npm/node_modules/supports-color/index.js using require().
[ Wasp !] Support for loading ES Module in require() is an experimental feature and might change at any time
[ Wasp !] (Use `node --trace-warnings ...` to show where the warning was created)
[ Wasp ] server/env.ts(82,5): error TS2783: 'NODE_ENV' is specified more than once, so this usage will be overwritten.

❌ --- [Error] Your wasp project failed to compile: -------------------------------

- SDK build failed with exit code: 2


❌ --- [Error] Compilation of wasp project failed: --------------------------------

1 errors found
🐝 --- Building SDK... ------------------------------------------------------------


[ Wasp !] (node:43832) ExperimentalWarning: CommonJS module /opt/homebrew/lib/node_modules/npm/node_modules/debug/src/node.js is loading ES Module /opt/homebrew/lib/node_modules/npm/node_modules/supports-color/index.js using require().
[ Wasp !] Support for loading ES Module in require() is an experimental feature and might change at any time
[ Wasp !] (Use `node --trace-warnings ...` to show where the warning was created)
[ Wasp ] server/env.ts(82,5): error TS2783: 'NODE_ENV' is specified more than once, so this usage will be overwritten.

❌ --- [Error] Your wasp project failed to compile: -------------------------------

- SDK build failed with exit code: 2


❌ --- [Error] Compilation of wasp project failed: --------------------------------

1 errors found
wasp 0.17.0
node v23.1.0
wasp 0.17.0
node v23.1.0
33 Replies
kapa.ai
kapa.aiβ€’3w ago
Hi @aspiringaiguy. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The error message you’re seeing:
server/env.ts(82,5): error TS2783: 'NODE_ENV' is specified more than once, so this usage will be overwritten.
server/env.ts(82,5): error TS2783: 'NODE_ENV' is specified more than once, so this usage will be overwritten.
indicates that the environment variable NODE_ENV is being defined multiple times, likely in your environment variable configuration files or in the way environment variables are being loaded into your Wasp project. Key Points from Wasp Documentation - In development, Wasp recommends using .env.server for server-side environment variables and .env.client for client-side variables. Each variable should only be defined once in the appropriate file. Duplicates can cause conflicts or unexpected behavior, as seen in your error message. - Environment variables set in the shell (e.g., via the command line) will override those in .env files, but again, each variable should only be set once per context to avoid overwriting issues. - The .env.server and .env.client files should be in the root of your Wasp project and not committed to version control by default. Make sure you don’t have duplicate definitions of NODE_ENV in these files or in your shell environment when running Wasp commands.
Defining Env Vars in Development Troubleshooting Steps 1. Check for Duplicate Definitions:
- Open your .env.server and .env.client files and search for multiple NODE_ENV entries. Remove any duplicates. - Check your shell environment (e.g., .bashrc, .zshrc, or the command line where you run Wasp) for NODE_ENV being set multiple times. 2. Node Version Compatibility:
- You are using Node v23.1.0, but Wasp’s latest stable releases are tested and supported on Node.js 18 and, in newer releases, Node.js 20. Using Node 23 may introduce compatibility issues. Consider switching to Node 18 or 20 using a version manager like nvm for best results
Node version requirements. 3. Clean and Rebuild:
- After removing duplicates, run wasp clean and then try building again with wasp build or wasp start. Summary - The error is due to NODE_ENV being defined more than once. - Remove duplicate definitions from your .env files and shell environment. - Consider using Node.js 18 or 20 for best compatibility with Wasp. If you continue to have issues after these steps, please provide the contents of your .env.server and .env.client files (with secrets redacted), and specify how you are setting environment variables in your shell. This will help further diagnose the problem. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @aspiringaiguy, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
aspiringaiguy
aspiringaiguyOPβ€’2w ago
I switched to node 20 according to this guide but I'm still getting the same exact error. Any help would really be appreciated. Also, NODE_ENV isnt defined in any of my env files. So I’m not sure how to fix this. @kapa.ai any pointers?
kapa.ai
kapa.aiβ€’2w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Based on the knowledge sources, the error you’re seeing:
server/env.ts(82,5): error TS2783: 'NODE_ENV' is specified more than once, so this usage will be overwritten.
server/env.ts(82,5): error TS2783: 'NODE_ENV' is specified more than once, so this usage will be overwritten.
suggests that the NODE_ENV environment variable is being defined multiple times, likely in your environment variable files or possibly in your shell environment. Pointers: 1. Check for Duplicate Definitions:
- Open your .env.server and .env.client files in your project root and ensure NODE_ENV is not defined more than once in either file.
- Also, check if you are setting NODE_ENV in your shell or in the command used to start/build your app, as environment variables set in the shell override those in .env files, and duplicates can cause this error
(Defining Env Vars in Development). 2. Use the Correct Files:
- For server-side variables (including NODE_ENV), use .env.server.
- For client-side variables, use .env.client and prefix them with REACT_APP_ (Env Vars Docs). 3. Node Version:
- While not directly related to the error, be aware that Wasp is tested primarily with Node 18 and 20. Using Node 23 may introduce unexpected issues. 4. Clean and Rebuild:
- After making changes, run wasp clean and then rebuild your project. If after these steps the issue persists, please double-check for any scripts or tools that might be injecting or redefining NODE_ENV during your build process. The knowledge sources do not provide more specific troubleshooting for this error, but the above steps are directly supported by the official Wasp documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @aspiringaiguy, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
aspiringaiguy
aspiringaiguyOPβ€’2w ago
@kapa.ai .env.server does not have NODE_ENV defined. I do not have a .env.client and printenv does not yield any explicit definitions of NODE_ENV. I have no idea where it is being set.
kapa.ai
kapa.aiβ€’2w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Thank you for the detailed follow-up. Based on the knowledge sources, here’s what we know: - Wasp expects environment variables for the server in .env.server and for the client in .env.client (Defining Env Vars in Development). - If NODE_ENV is not present in your .env.server, you do not have a .env.client, and printenv does not show it, then it is likely being set internally by Wasp or by a dependency during the build process. - There is no explicit documentation in the provided sources about Wasp itself setting NODE_ENV multiple times, but there is a mention that environment variables can be set in multiple places (shell, .env files), and that conflicts can arise if the same variable is set more than once (Defining Env Vars in Development). What you can do: - Double-check for any scripts, npm/yarn commands, or CI/CD configurations that might be setting NODE_ENV. - Review your package.json scripts and any custom build scripts for duplicate NODE_ENV assignments. - Since the error is coming from server/env.ts, inspect that file (or its template) for multiple assignments of NODE_ENV. Limitation:
The knowledge sources do not provide a definitive answer as to where NODE_ENV is being set in your specific case if it is not in your .env.server, .env.client, or shell environment. There is no mention of Wasp itself setting NODE_ENV more than once by default. If you continue to encounter this issue, you may want to open an issue on the Wasp GitHub or ask in the Wasp Discord for more targeted help, as this may be a bug or an edge case not covered in the current documentation. Let me know if you’d like guidance on how to further debug this within your project files! ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @aspiringaiguy, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
genyus
genyusβ€’2w ago
What command are you running for your local build? And have you tried running wasp clean?
aspiringaiguy
aspiringaiguyOPβ€’2w ago
Hello @genyus , yes I try wasp clean then wasp start.
genyus
genyusβ€’2w ago
If you do a global search for NODE_ENV= (i.e. set ** as the files to include), how many results do you get back? And also, is this a problem that started occurring after some specific change?
MEE6
MEE6β€’2w ago
Wohooo @aspiringaiguy, you just became a Waspeteer level 2!
aspiringaiguy
aspiringaiguyOPβ€’2w ago
As far as I know printenv prints all declared env vars where none are NODE_ENV. Is this what you mean by a global search? Also, none of my env files declare its value. What changes did I make? Well I had faced an issue with fly io when redeploying that led me to migrating from 0.15.0 to 0.18.0. Miho suggests here to migrate to 0.16.0. I had done that but still faced issues, so I upgraded again to 0.18.0 where I am currently facing this bug.
genyus
genyusβ€’2w ago
printenv isn't doing what you think, here. It outputs your system env vars, but it won't take your static Wasp env vars into account. What I mean is to run a global search for that term in your project in your IDE. The ** file pattern is so that the search includes hidden folders like .wasp
aspiringaiguy
aspiringaiguyOPβ€’2w ago
Gotcha. The file that has the error is .wasp/out/sdk/wasp/server/env.ts: The error is from here:
// PUBLIC API
export const env = ensureEnvSchema(
{ NODE_ENV: serverDevSchema.shape.NODE_ENV.value, ...process.env },
serverEnvSchema,
)
// PUBLIC API
export const env = ensureEnvSchema(
{ NODE_ENV: serverDevSchema.shape.NODE_ENV.value, ...process.env },
serverEnvSchema,
)
Error message:
'NODE_ENV' is specified more than once, so this usage will be overwritten.

This spread always overwrites this property.

const serverDevSchema: z.ZodObject<{
NODE_ENV: z.ZodLiteral<"development">;
WASP_SERVER_URL: z.ZodDefault<z.ZodString>;
WASP_WEB_CLIENT_URL: z.ZodDefault<z.ZodString>;
JWT_SECRET: z.ZodDefault<z.ZodString>;
}, "strip", z.ZodTypeAny, {
...;
}, {
...;
}>
'NODE_ENV' is specified more than once, so this usage will be overwritten.

This spread always overwrites this property.

const serverDevSchema: z.ZodObject<{
NODE_ENV: z.ZodLiteral<"development">;
WASP_SERVER_URL: z.ZodDefault<z.ZodString>;
WASP_WEB_CLIENT_URL: z.ZodDefault<z.ZodString>;
JWT_SECRET: z.ZodDefault<z.ZodString>;
}, "strip", z.ZodTypeAny, {
...;
}, {
...;
}>
aspiringaiguy
aspiringaiguyOPβ€’2w ago
Full file
aspiringaiguy
aspiringaiguyOPβ€’2w ago
Removing NODE_ENV: serverDevSchema.shape.NODE_ENV.value, seems to allow the build to succeed. Tbh not sure if this is a good fix or a simple workaround for a deeper issue. This fix only works for the local build. But wasp deploy fly deploy fails as it automatically overrides any changes I make to env.ts any suggestions @kapa.ai ?
kapa.ai
kapa.aiβ€’2w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Based on the knowledge sources, here are some concrete suggestions: 1. Check for Duplicate Definitions in All Possible Places:
Even though you’ve checked .env.server, .env.client, and your shell environment, also review: - Any scripts in your package.json that might set NODE_ENV. - Any build or deployment scripts (e.g., CI/CD pipelines) that could be injecting NODE_ENV. - The generated files in the .wasp directory, especially server/env.ts, to see if NODE_ENV is being set or imported more than once. 2. Clean and Rebuild:
Run wasp clean before building again. This ensures that any stale or duplicate environment files are removed, as Wasp has had issues in the past with leftover .env files causing unexpected behavior (Github Issue #209). 3. Node Version:
While not directly related to the duplicate variable, Wasp is primarily tested with Node 18 and 20. Using Node 23 may introduce unexpected issues, so consider switching to a supported version if possible. 4. Check for Internal or Dependency Issues:
The knowledge sources do not provide explicit information about Wasp or its dependencies setting NODE_ENV multiple times by default. If you cannot find the duplicate definition in your codebase, it may be coming from a dependency or from Wasp’s internal code generation. In this case, consider opening an issue on the Wasp GitHub or asking in the Wasp Discord for further assistance, as this may be an edge case or bug (Github Issue #175). Summary:
- Double-check all possible sources of NODE_ENV definition. - Clean and rebuild your project. - Consider using a supported Node version. - If the issue persists and you cannot find the source, reach out to the Wasp community for more targeted help. The knowledge sources do not contain more specific troubleshooting steps for this exact error, so if these suggestions do not resolve your issue, further investigation with the Wasp team may be necessary. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @aspiringaiguy, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
aspiringaiguy
aspiringaiguyOPβ€’2w ago
@kapa.ai the issue is that the auto generated env.ts includes multiple definitions of NODE_ENV. Fixing this issue means preventing the script auto-generating env.ts from making this mistake. Any clue how to go about this? What is causing env.ts to include NODE_ENV in process.env? To re-iterate, I do not define NODE_ENV anywhere in my main app.
// PUBLIC API
export const env = ensureEnvSchema(
{ NODE_ENV: serverDevSchema.shape.NODE_ENV.value, ...process.env },
serverEnvSchema,
)
// PUBLIC API
export const env = ensureEnvSchema(
{ NODE_ENV: serverDevSchema.shape.NODE_ENV.value, ...process.env },
serverEnvSchema,
)
kapa.ai
kapa.aiβ€’2w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Based on the knowledge sources, there is no explicit documentation or issue describing the exact mechanism by which Wasp's code generation process causes multiple NODE_ENV definitions in the auto-generated env.ts. However, there are some relevant insights: - Wasp generates environment variable handling code, and there have been discussions about validating and collecting env vars in a single place, ideally via a config import rather than direct process.env usage. There is also mention of using Zod schemas for validation and ensuring all variables are listed and validated centrally (Github Issue #1067). - The error you are seeing ('NODE_ENV' is specified more than once, so this usage will be overwritten.) suggests that the code generation logic is merging an object that already contains NODE_ENV (from the schema or defaults) with process.env, which also contains NODE_ENV, resulting in a duplicate key in the object spread. The relevant code you posted:
export const env = ensureEnvSchema(
{ NODE_ENV: serverDevSchema.shape.NODE_ENV.value, ...process.env },
serverEnvSchema,
)
export const env = ensureEnvSchema(
{ NODE_ENV: serverDevSchema.shape.NODE_ENV.value, ...process.env },
serverEnvSchema,
)
shows that NODE_ENV is being set explicitly and then all of process.env is being spread in, which will include NODE_ENV again if it exists in the environment. What is causing this? - The code generation logic is explicitly adding NODE_ENV and then also including all of process.env, which will almost always include NODE_ENV (since Node sets it by default). - This results in two definitions of NODE_ENV in the same object, which TypeScript warns about. How to fix it? - The fix would be in the code generation logic: ensure that NODE_ENV is only set once in the object passed to ensureEnvSchema. For example, you could omit NODE_ENV from the spread of process.env if you are already setting it explicitly, or avoid setting it explicitly if you want to rely on process.env. Summary:
The root cause is the code generation logic that merges an explicit NODE_ENV with process.env, which also contains NODE_ENV. The knowledge sources do not provide a direct fix, but the solution is to adjust the code generation so that NODE_ENV is only included once in the object passed to ensureEnvSchema. If you are not modifying the code generation yourself, this should be reported as a bug to the Wasp team, as it is not expected behavior for the generated code to include duplicate keys. If you want to patch this locally, you could modify the generated env.ts to avoid the duplicate, but for a permanent fix, the code generation logic in Wasp itself needs to be updated. The knowledge sources do not provide the exact location of this logic, so further investigation in the Wasp codebase or reaching out to the Wasp maintainers may be necessary. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @aspiringaiguy, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
aspiringaiguy
aspiringaiguyOPβ€’2w ago
@genyus sorry to bother you, but any thoughts on this?
genyus
genyusβ€’2w ago
This code is as expected, but as you surmised, something is adding the NODE_ENV property to your process.env object at compile time, which is why Typescript is throwing that error. This suggests that something in your codebase must be augmenting the ProcessEnv interface and I was able to reproduce the error locally by adding a global.d.ts file like this:
declare namespace NodeJS {
interface ProcessEnv {
NODE_ENV: "development" | "production";
}
}
declare namespace NodeJS {
interface ProcessEnv {
NODE_ENV: "development" | "production";
}
}
There must be some code in your application applying a similar augmentation. Do you see any TS errors in your env.ts file when you open it in your IDE? If so, then you need to find and remove the property in that interface declaration.
aspiringaiguy
aspiringaiguyOPβ€’2w ago
The only error in env.ts comes from this snippet:
// PUBLIC API
export const env = ensureEnvSchema(
{ NODE_ENV: serverDevSchema.shape.NODE_ENV.value, ...process.env },
serverEnvSchema,
)
// PUBLIC API
export const env = ensureEnvSchema(
{ NODE_ENV: serverDevSchema.shape.NODE_ENV.value, ...process.env },
serverEnvSchema,
)
Removing NODE_ENV like below does fix it, but this fix does not help with deploying to fly as my edits get over-written.
// PUBLIC API
export const env = ensureEnvSchema(
{ ...process.env },
serverEnvSchema,
)
// PUBLIC API
export const env = ensureEnvSchema(
{ ...process.env },
serverEnvSchema,
)
Any edits I make to env.ts are not persistent. What is adding NODE_ENV to process.env? I really can't find the source and how can it be my code? I have 0 declarations of NODE_ENV in my application code and env files. Note: when I mention env.ts I mean the one under .wasp. I don't have a env.ts under my application. Is this something I should have?
genyus
genyusβ€’2w ago
No, you shouldn't have that file in your source, it's automatically generated. As I explained before, for Typescript to be throwing that error, the ProcessEnv interface must've been augmented, meaning that it has NODE_ENV declared as a static property. If you don't have any such references in your own code, then it may be applied by some dependency you've imported. Do you have Typescript Intellisense configured in your IDE?
aspiringaiguy
aspiringaiguyOPβ€’2w ago
I believe so yes
MEE6
MEE6β€’2w ago
Wohooo @aspiringaiguy, you just became a Waspeteer level 3!
aspiringaiguy
aspiringaiguyOPβ€’2w ago
I finally understand @genyus . The next dependency did set NODE_ENV in its ProcessEnv interface. Found this by modifying env.ts to process.env.NODE_ENV so I can follow NODE_ENV and see where it is defined. Then I deleted it. If you have the time I would like your opinion on why I faced this issue to start? How come there is no metion of this issue anywhere in discord or github? It must be something I did, but what? node_modules includes next but I didn't add it in my package.json
genyus
genyusβ€’2w ago
I don't know how Next ended up in your project dependencies, but Wasp most definitely doesn't include it and the two frameworks shouldn't be expected to work well together. The reason you haven't seen this issue raised anywhere is because this isn't a situation that would occur in normal circumstances, your setup is an anomaly
aspiringaiguy
aspiringaiguyOPβ€’2w ago
Thanks for explaining. I think my other project is conflicting with this one. I'll see if I can set up an isolated env so this doesn't happen again (if you have any recommendations I would love to hear them). Thank you so much for your help!
genyus
genyusβ€’2w ago
Could you explain more about how you've got your projects organised? Local Next and Wasp projects shouldn't interfere with each other if they're in separate project directories
aspiringaiguy
aspiringaiguyOPβ€’2w ago
Then I have no idea how this happened. Each project is in its own subdir. My root dir is codebases under which different projects exist. Should they not be neighbours?
genyus
genyusβ€’2w ago
It shouldn't be a problem for them to be neighbours, no. If you use Git in your dev workflow, then you can check the history to see where the change got introduced. If you don't, then that would be my first recommendation
aspiringaiguy
aspiringaiguyOPβ€’2w ago
Yeah good idea. I’ll check my git history and see if I can track this down. Thanks again for all the help πŸš€πŸš€πŸš€
genyus
genyusβ€’2w ago
You're welcome!
miho
mihoβ€’2w ago
@genyus thanks for picking this one up πŸ‘
@aspiringaiguy thank you for debugging the issue and figuring out that it was a next dep that messed with the env vars Great detective work here πŸ‘
miho
mihoβ€’2w ago
Written this down as a Wasp bug here: https://github.com/wasp-lang/wasp/issues/3188
GitHub
Build fails if process.env.NODE_ENV is typed by deps Β· Issue #31...
wasp start fails with TS2783: &#39;NODE_ENV&#39; is specified more than once, so this usage will be overwritten when ProcessEnv interface is augmented with NODE_ENV property. The explanation Wasp g...

Did you find this page helpful?