Question

I started using Browserify and not sure if I completely understand how to use it.

I have a file with some functions bundled in one object in foo.js

var foo = {
  f1: function(){...}
  f2: function(){...}
}

module.exports = function () {
  return foo;
};

And I want to export them to a variable in the main.js file, so I tried doing this:

var bar = require('/foo')();

The goal is to be able to do bar.f1(). Without executing require('/foo') I get only a function definition, so I have to execute it. Am I doing something wrong?

Was it helpful?

Solution

Just export the object:

var foo = {
  f1: function(){...}
  f2: function(){...}
};

module.exports = foo;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top