Skip to content

Instantly share code, notes, and snippets.

@kesne
Last active April 20, 2017 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kesne/345ed393ceabb495f633554bafea0781 to your computer and use it in GitHub Desktop.
Save kesne/345ed393ceabb495f633554bafea0781 to your computer and use it in GitHub Desktop.
global.env

Why?

There's no standardized, cross-platform, developer-friendly way to detect what environment you're running code in. The JS community has generally centered around reading process.env.NODE_ENV, but this property feels out of place in browser code, and requires a lot of typing at every usage (it's too easy to get wrong or flip the condition backwards). Some people set up __DEV__ to transpile down to process.env.NODE_ENV !== 'production' for a better developer experience.

Enter global.env

A new property, hung on the global object. This spec is to satisfy node, browser, and native.

What is it?

This is still in discussion, but seems to fall into two camps:

Clean replacement for NODE_ENV

In this proposal, global.env is a string that resolves to the NODE_ENV.

if (global.env === 'production') {
  // use fast path...
}

Environment Object

In this proposal, global.env is an object with helpers to quickly determine the environment the code is running in.

if (global.env.production) {
  // use fast path...
}

Why global?

Using global means that it'll work where ever process.env is being used today, and global is on the standards track, and will likely hit browsers as well. Hanging a property on global ensures that the developer experience is solid, without.

Why env?

There's no current env property on global in Node, WebPack, or Browsers. Therefore, the property should be web-safe. It also aligns with prior process.env.NODE_ENV usage cleanly, and provides clear signal of what it does to developers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment