Skip to content

Instantly share code, notes, and snippets.

@alexilyaev
Last active March 7, 2023 09:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexilyaev/5c73dcdbcb429a1e0c081627548b7ca3 to your computer and use it in GitHub Desktop.
Save alexilyaev/5c73dcdbcb429a1e0c081627548b7ca3 to your computer and use it in GitHub Desktop.
.env vs. node-config

.env vs. node-config

Pros

  • Very simple to setup.
  • Automatically assigns configured values on top of process.env.
  • Automatically supports overriding configured values via Environment Variables.
  • Promotes best practices (FAQ):
    • Have only one config file.
    • Don't commit the file to version control.
  • Supported by Create React App out of the box.

Cons

  • Can't use dynamic values. e.g.:
    • Referencing other variables (can use dotenv-expand for that).
    • Importing constants from another file.
    • Setting a value by running a JavaScript expression like Date.now().
  • All values are translated into strings.
    • That's just how process.env behaves when assigning values on it. e.g.:
      process.env.ENABLED = true;
      process.env.NUM = 3;
      console.log(process.env.ENABLED, process.env.NUM);
      // 'true' '3'

Pros

  • Very flexible.
  • Can use any data structure.
  • Supports overriding configured values via Environment Variables (although requires a mapping config).
  • Supports multi deployment/instance configs out of the box.

Cons

  • Doesn't automatically map config values on process.env.
    • That's because it takes a different approach to configs.
  • Environment Variables overrides require a non trivial mapping config (Custom Environment Variables).
  • Doesn't support ES Modules syntax (although TypeScript is supported).
@alexilyaev
Copy link
Author

Conclusion

Prefer using .env as it's much simpler to setup and covers most use cases, especially for Frontend projects.
Use node-config only if you need complex or multi deployment/instance configs.

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