Skip to content Skip to sidebar Skip to footer

Require In Browserify Doesn't Work Variable Name

I am trying to require a file with browserify using variables passed into a function: var playersOptions = { name: 'players', ajax: 'team-overview', route: { na

Solution 1:

Browserify has to be able to statically analyze all of the require statements at build time so it can know what files it needs to include in the bundle. That requires that require can only be used with a string literal in the source code.

Instead of passing the name of a module around to require later, just pass the module itself:

varplayersOptions= {
    name: 'players',
    ajax: 'team-overview',
    route: {
        name: 'overview',
        module: require('playersOverview'),
        url: 'playersoverview'
    }
 };

varBackboneView= playersOptions.route.module;

Even if this Browserify limitation wasn't present (eg. if you were using node.js directly), it's still a good idea to avoid passing module names to be required later because the require call could break if the module name passed to it had a path relative to the caller's directory and was passed into code in a file inside a different directory.

Post a Comment for "Require In Browserify Doesn't Work Variable Name"