Reputation: 29159
I have a javascript class which can be used in both the browser and NodeJS.
Suppose the class looks like this
class BarFoo {
lookup() {
// Do magic
if (magicFailed && window.DEVELOP) {
console.log('Show some info to help debugging...');
}
}
...
}
To make this example also work for NodeJS I can imaging that the global
In this example I only show the log message if the app is in developing mode (window.DEVELOP === true
). But I would like to use this class also in a nodeJS app, so I don't have a window
object. What is the preferred way to set this up for both?
Upvotes: 0
Views: 1134
Reputation: 2995
You could do it like this:
Start your app with:
NODE_ENV=development node app.js
Then check it like so:
const isServer = typeof window === 'undefined'
const isDev = isServer ? process.env.NODE_ENV === 'development' : window.DEVELOP
if (isDev) {
// ...
}
Upvotes: 1