6

What's the best practice for using external code, e.g. code found in node modules, in angular?

I'd like to use this https://www.npmjs.com/package/positionsizingcalculator node module in my angular app. I've created an angular service intended to wrap the node module, and now I want to make the service use the node module.

'use strict';

angular.module('angularcalculator')
  .service('MyService', function () {
    this.calculate = function () {
      return {
        //I want to call the node module here, whats the best practice?
      };
    }
  });
3
  • Is it safe to assume that your backend is a Node application? Commented Apr 17, 2015 at 23:25
  • @Michael Benford Would you mind elaborating on what the difference would be? Commented Apr 18, 2015 at 1:16
  • I just wanted to know whether you're serving your Angular app from a Node application, so I could suggest something like Browserify (as @Dylan Watt did). Commented Apr 18, 2015 at 1:32

1 Answer 1

5

To do this, I would crack open the package and grab the .js out of it. This package is MIT license, so we can do whatever we want. If you navigate to /node_modules/positionsizingcalculator/ you'll find an index.js. Open that up and you'll see the moudle export, which takes a function that returns an object.

You'll notice this is an extremely similar pattern to .factory, which also takes a function that returns an object (or constuctor, depending on your pattern). So I'd do the following

.factory('positionsizingcalculator', function(){
        basicValidate = function (argument) {
        ... //Insert whole declaration in here
        return position;
}) 

and the inject it where you need it:

.controller('AppController', function(positionsizingcalculator){
    //use it here as you would in node after you inject it via require.
})

-- Edit: This is good for one off grabs of the JS, but if you want a more extensible solution, http://browserify.org/ is a better bet. It allows you to transform your requirements into a single package. Note that this could result in pulling down a lot more code that you might otherwise need, if you make one require bundle for your whole site, as this is not true AMD, and you need to to load everything you might want one the client, unless you make page specific bundles.

You'd still want to do the require in a factory and return it, to keep it in angular's dependency injection framework.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm sure this would "work" but it seems a poor solution for several reasons. I want to actually use the node module, not manually copy the code out of it.
See my edit for a more extensible solution. I'd still just grab the code if its just a one time issue, but if you see yourself wanting to use a lot of node modules, browserify could be a good solution. Ideally, most of you packages are coming from something like bower, though, and you wouldn't need it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.