question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Should add process.env.NODE_ENV to client types.

See original GitHub issue

Vite types should define process.env.NODE_ENV

process.env.NODE_ENV should be defined by vite references since it’s inherently relevant.

I know I can easily import it myself, or add it to tsconfig. That being said I like to keep keep my global namespaces unpolluted so I tend to go with explicit imports as much as I can. Like

"typeRoots": [],
"types": [],

Statically replacing NODE_ENV at compile time is one of the main ways to eliminate dead code since it’s easier to compare "production" === "production" than variable === "production". And since Vite is a build tool, it should supplement the type.

Suggested solution

Something like

declare namespace NodeJS {
  export interface ProcessEnv {
    NODE_ENV: string 
  }
}

inside /// <reference path="./types/importMeta.d.ts" />

Alternative

Could use tsconfig or declaration merge in a local file.d.ts to do it myself, but I actually find it comforting that vite gives me everything relevant to build tools out of the box.

I got a BuildTimeTypes.d.ts files with

/// <reference types="vite/client" />
/// <reference path="./types/importMeta.d.ts" />
/// <reference types="vitest/importMeta" />

interface ImportMetaEnv { 
  readonly RUNTIME_VERSION: string
  readonly RUNTIME_REACT_VERSION: string
  readonly RUNTIME_BUILD_SHA: string
  readonly RUNTIME_BRANCH: string
  readonly RUNTIME_BUILD_DATE: string
  readonly RUNTIME_CONFIG_ASSETS_LIMIT: string

}

Seems like it would be perfect inside "./types/importMeta.d.ts"

Additional context

I don’t mind doing the PR.

No response

Validations

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
seivancommented, Jun 13, 2022

That’s beyond the point. Vite is using process.env.NODE_ENV internally. React is using it. Every single library you got… is using it.

You can make the claim that import.meta.env will eventually replace it, but that is not your case today. Not to mention it’s nice having ONE standard used for shaking dead code off.

I use import.meta.env to assign for runtime conditions, but for anything that that’s absolute I will be using process.env.NODE_ENV, because it’s transparent and tangible. I can set it myself without worrying about the conditions the build system does to decide wether or not we’re “dev” or “prod”, and I can expect the dependencies used to do the same.

3reactions
bluwycommented, Jun 13, 2022

process.env.NODE_ENV is a standard in nodejs only. There’s no process in the browser, hence Vite introduce import.meta.env for it. It’s common for libraries to use them, even if they are for browser usage, but that was because exports didn’t exist then (production and development conditions are preferred now). But for project source code that’s written for browsers, it should be avoided.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.js pass NODE_ENV to client - Stack Overflow
I'd like to define the NODE_ENV as a window variable when I first render the page on the server, and then in my...
Read more >
Working with Environment Variables in Node.js - Twilio
Environment variables are a great way to configure parts of your Node.js application. Learn how to work with them using helpful tools such ......
Read more >
Building Better Bundles: Why process.env.NODE_ENV ...
NODE_ENV is a system environment variable that Node exposes into running scripts. It's used by convention to determine dev-vs-prod behavior, by ...
Read more >
Basic Features: Environment Variables - Next.js
Note: In order to keep server-only secrets safe, environment variables are evaluated at build time, so only environment variables actually used will be ......
Read more >
process.env: What it is and why/when/how to use it effectively
The process.env global variable is injected by the Node at runtime for your application to use and it represents the state of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found