Browserify: require doesn't work when path is a variable

Created on 20 Feb 2014  路  4Comments  路  Source: browserify/browserify

I am trying to do something like this

// config.js
var path = require("path"),
    env = process.env.NODE_ENV || "fail";

module.exports = require(path.join(__dirname, env));

Some file that uses the config

// app.js
var config = require("./config");
console.log(config);

And bundle with this command

NODE_ENV=test browserify -d app.js > public/test/app.js

Browserify is not picking up the NODE_ENV assignment and it's trying to include fail environment when I run my testing bundle :frowning:

Uncaught Error: Cannot find module '/config/fail' 

Most helpful comment

Usually dynamic dependencies are not necessary :smile:

All 4 comments

browserify doesn't work with conditional requires and can't be made to work with arbitrary dynamic requires without solving the halting problem first. Do something different instead. This is a dead end.

:flushed: a response from the master!

Thank you for such a pragmatic response. I'll do a little more research and see what other things I can do to solve the problem.

@naomik have a look at the browser-field to load different modules in the browser than in node.

If you need more power, i.e. swap out modules for different environments browserify-swap may help you, but you gotta have a real good reason to do that (kinda hacky).

Usually dynamic dependencies are not necessary :smile:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rkirsling picture rkirsling  路  6Comments

timwis picture timwis  路  4Comments

ericmorand picture ericmorand  路  6Comments

QuantumKing picture QuantumKing  路  9Comments

ivankarpey picture ivankarpey  路  7Comments