hughsk / envify

:wrench: Selectively replace Node-style environment variables with plain strings.
902 stars 57 forks source link

Including envify as a transform causes browserify to throw an error #32

Closed toddself closed 9 years ago

toddself commented 9 years ago

This is odd.

If I do:

browserify -t envify entrypoint.js

it will work.

However, if I do: browserify('entrypoint.js').transform(require('envify')()).bundle()

browserify will crash with:

Unable to build /home/user/scripto2/src/static/js/entrypoint.js path must be a string Error: path must be a string
        at /home/user/scripto2/node_modules/browserify/node_modules/resolve/lib/async.js:16:16
        at doNTCallback0 (node.js:407:9)
        at process._tickDomainCallback (node.js:377:13) { err:
       { [Error: path must be a string]
         stream:
          Labeled {
            _readableState: [Object],
            readable: true,
            domain: null,
            _events: [Object],
            _eventsCount: 4,
            _maxListeners: undefined,
            _writableState: [Object],
            writable: true,
            allowHalfOpen: true,
            _options: [Object],
            _wrapOptions: [Object],
            _streams: [Object],
            length: 1,
            label: 'deps' } } }

If I remove the .transform call everything works correctly.

browserify 10.2.6 (as well as 11.0.1) envify 3.4.0

I've created a simple test case here: https://github.com/toddself/envify-example

if you do node bundle.js you'll see the error. if you remove the call to .transform it'll work. (it doesn't matter if you put envify in the .transform call or in the transform keys in package.json -- they both fail). However, if you do node_modules/.bin/browserify -t envify entrypoint.js it'll work...

yoshuawuyts commented 9 years ago

This is todd.

FTFY :grin:

But to answer your question: could you try doing require('envify/custom'), I suspect that will resolve your issue.

toddself commented 9 years ago

:laughing:

Ah @yoshuawuyts you are my hero this morning!

Thanks again!

toddself commented 9 years ago

But after looking at the code I'm confused as to why this works?

It looks like including the base project actually calls custom with process.env, where as calling custom directly with no args short-circuits into process.env.

Any idea on why this actually works?

yoshuawuyts commented 9 years ago

I believe the files are used as follows:

Not entirely sure why this is how it is, pretty sure we could do a better job (':

damncabbage commented 8 years ago

For someone who stumbled across this ticket with the same confusing problem:

From the example:

browserify('entrypoint.js').transform(require('envify')()).bundle()

Should actually be:

browserify('entrypoint.js').transform(require('envify')).bundle()
         // No () here; it's already ready for use ---^

(The default version of envify is already "applied". I think this was a little unclear from the docs; the notation in the "Module Usage" headings was unfamiliar to me.)

zekedroid commented 7 years ago

@damncabbage: saved me after hours of debugging. This should go into the README.md.