How to access process.env.NODE_ENV on the client?


#1

I really need to access process.env.NODE_ENV on the client, pretty much before anything else fires off. I tried using a Meteor method to fetch it, but it’s asynchronous, and so there are other things happening on the client (Google Analytics, Facebook pixel, etc) that shouldn’t happen till it’s determined if the app is in production or not.

How can I check if my app is in production or development, before anything else happens?


#2

Easiest solution is to use a public setting.

settings/production.json:

{
  "public": {
      "isProduction": true
   }
}

Then you can use Meteor.settings.public.isProduction


#3

If it should be accessible publicly for everybody, not just admins etc than yes, settings.json works.

In the other usecase I would probably try something like that meteorhacks streams kinda server-client collections without DB behind them.
It would check current user on server and return needed info based on user privilegues.
Paired with fast-render, it could push that info same way as user account is - even before any other JS is loaded.

So to sum it up - low level publication which would just check user privileges and return something to client only collection. That would be probably my way.


#4

The other way is to use package with debugOnly which save something in your namespace. If you are following package based architecture, you probably have some server/client object defined in your lib or core package which define your app namespace.

MyApp = {};
MyApp.VERSION = 0.0.1

So than just add debug flag there(into debug package).

MyApp.DEBUG = true

If you pack for production, debugOnly package is left out, so that flag would not be set.


#5

Cool - thanks for the ideas! I like the simple package in debugMode option. It’s lightweight, and it’s sure to be present before anything else.


#6

May I give a hint to our packages https://github.com/4commerce-technologies-AG/meteor-package-env-settings and https://github.com/4commerce-technologies-AG/meteor-package-meteor-namespace-template-helper

The benefit from that is that you might not only set NODE_ENV but you can cascade your settings AND informational properties by yaml (prefferred) or json grammar.

That means that depending on your NODE_ENV the package will load and merge a different set of files and values to server and client. With the helper package you may access the values directly in forms and scripts.


#7

yes, we learn something new every day when trying to make our app kinda production ready.

and I would also suggest any demo data seeding be part of debugOnly package, it does not look good on production :smiley:


#8

You can use a helper package like manual:isdev that exposes the variable Meteor.isDev, which you can use like so:

Meteor.startup(() => {
    if (!Meteor.isDev) {
        // do stuff
    }
});