دنبال کننده ها

۱۳۹۶ اسفند ۱۲, شنبه

node.js - Nodejs best practice for setting environment variables

[ad_1]



In nodejs, it's common to set



process.env.NODE_ENV = 'development' // or 'production'


This helps dead code elimination, such as when using Uglifyjs, webpack



What about for testing, for targeting browser, or server etc?



process.env.NODE_ENV = 'test' // is this good?
process.env.TEST = true // or is this better?

process.env.APP_ENV = 'browser' // browser specific codes when building "universal" app
process.env.APP_ENV = 'server' // server specific codes
process.env.APP_ENV = 'node' // or this for server
process.env.BROWSER = true // or this
process.env.SERVER = true //


I can think of these:



Using process.env.NODE_ENV = 'test'




  • Simple, only 1 env variable to set

  • But may break other third parties' tool, such as babel, which requires either development or production



Using process.env.TEST = true




  • Doesn't interfere with NODE_ENV

  • But more env variables to set and remember (mental load), need to set DefinePlugin (such as when using webpack)

  • Again, some other third parties may have use process.env.TEST = somethingElse



What are other pros and cons of these approaches and are there existing "best practices"?




[ad_2]

لینک منبع