4

So I've been playing with JS and browserify to allow to split my JS into smaller file chunks. It works great, however, I'm still lost on how to properly use the require function.

For me, it acts as a the Service Locator, because it looks for the proper "file" to load, and return an object. (For example in PHP, require somewhat load the file in the memory but doesn't construct).

Example:

var Foo = function() {
    console.log("I'm the Foo object");
};

module.exports = Foo;

Then, to use it I'll do:

var Foo = require('foo');

and

var foo = new Foo();

Note, that the exported function is NOT constructed.

I could have done:

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

None of those methods seems right to me (I may are wrong).

  • 1) Is it common to do it like this? Or should exported the executed function?

Anyway, this introduction is to understand how I should play with the require function.

For example if I've a Foo object, which is depends of Bar, I've two way to do:

Service Location:

var Foo = function() {
   var Bar = require('bar')();

   Bar.doSomethingAwesome();
};

module.exports = Foo;

or I can do:

Dependency Injection

var Foo = function(bar) { 
   bar.doSomethingAwesome();
};

module.exports = Foo;

// And at a latter time

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

I obviously know that that's two different things and serve different purposes.

  • 2) But I'm wondering what is the common/right way to do in JS, is there any commonly admitted rules?
Trent
  • 5,785
  • 6
  • 32
  • 43

3 Answers3

0

Browserify allows you to program with modules, there's not much more to it. It's not really a DI container or service locator per se, although you can probably make it work like one.

So doing this is perfectly fine:

var Foo = require('foo');
var foo = new Foo();

In that case, it makes sense to simply place all require calls at the top of your file, similar like you would do with using in C# or import in Java. I personally wouldn't scatter require calls since don't help much with readability.

You can also export an instance which doesn't have to be newed up anymore, as long as that is appropriate for what you want to do (in that case module.exports = Foo() would lead to a singleton).

Also see this related question: Dependency Injection with RequireJS

Community
  • 1
  • 1
Lars Höppner
  • 18,252
  • 2
  • 45
  • 73
0

The rewire library provides module injection for Node.js. A couple of solutions have been discussed and presented in this Github issue to support browserify.

Usage example from the project README:

var myModule = rewire("../lib/myModule.js");
myModule.__set__("fs", fsMock); // set private variable
Lucas Cimon
  • 1,859
  • 2
  • 24
  • 33
  • There's also [proxyquire](https://github.com/thlorenz/proxyquire), but both rewire and proxyquire work only with Node.js and not with browserify. – mik01aj Feb 18 '15 at 13:47
0

Use Browserify so you can require npm packages from your browser just like node. Then you can use Weather.js or require it, then inject it in any way you like.

Zac
  • 2,201
  • 24
  • 48
Muffasa
  • 218
  • 4
  • 12