You can modify the entry point file, which will basically do this setup, and then require the original main file.
process.env.NODE_ENV = 'production'; require('app.js');
Another way (imo much cleaner) is to use a conversion like envify that directly replaces your NODE_ENV in the code with a string value.
Option 1
I think your approach should usually work, but I would write directly to process.env , as I am sure that it is being overwritten in the kit. Instead, you can make a global variable like __env , and then in the actual package code, set it to process.env in your input file. This is an untested solution, but I believe it should work.
Option 2
Use localStorage and let the main script read the variables from there on initialization. You can set the variables in localStorage manually, or you can even give them to the server if you have them. The developer will simply open the console and type something like loadEnv('production') , it will do XHR and save the result in localStorage. Even with a manual approach, there is still the advantage that they do not need to hardcode in html.
If the manual does not look good enough, and the server is also a dead end, you can simply include all the variables from all environments (if they exist somewhere) in the set, and then use the switch to select the correct ones under certain conditions (for example, localhost, host manufacturer).
When you think about it, you are certainly outside the scope of Browserify with your needs. He can make a package for you, but if you do not want to receive this information in a bundle, you are on your own.
Fredyc
source share