hughsk/envify

Not replacing library environment variables

Closed this issue ยท 6 comments

I'm trying to use this transform on a project, but the transform doesn't seem to be replacing the environment variables within a library (specifically React, but I don't know if that makes a difference here).

To reproduce:

mkdir eg
cd eg

npm install -g browserify
npm install react envify
echo "require('react');" > file.js

# This ought to return no results, but it does
browserify -t [ envify --NODE_ENV production ] file.js | grep NODE_ENV

I have the exact same issue.

This means that production builds that require('react') still have React console.warn() in the console ...

You need to use a global transform for envify to alter a package's code โ€“ by default, local transforms are only applied to your application and not to anything in node_modules.

# Note the use of -g in place of -t
browserify -g [ envify --NODE_ENV production ] file.js

That being said, I believe envify is applied to React automatically when it's bundled, in which case the problem you have here is that you're using command-line arguments instead of environment variables. They don't propagate their way downwards to react's transform if they're passed the same way as in your example.

I think the best approach would actually just be:

NODE_ENV=production browserify file.js

Ah okay, I thought I was missing something obvious, but I couldn't find the answer to this anywhere. I'm actually using this as part of a gulp pipeline, so I'm avoiding setting environment variables like this in favor of just having a --production flag.

I am using gulp browserify. What is the equivalent of the "global" flag there? Anybody knows?

@levino: excerpt from my gulpfile,js:

browserify(...)
    ...
    .transform('envify', {
        global:   true, // also apply to node_modules
        NODE_ENV: debug ? 'development' : 'production',
    })
    ...
    .bundle()
    ...
    .pipe(gulpif(!debug, babelMinify())) // my example uses gulp-babel-minify
    ...

Meanwhile I switched to webpack. Thanks for finally answering my question though @stefanb2