0

Supposing I have a library written in Javascript to be used in a MEAN stack application.

I wish to use my library both from NodeJS and within Angular. To remain idiomatic I would like the library to appear as a Module inside node and as a Service within Angular.

the only way I can think to do this is to do something like:

var myLibrary = function(){
    var myLib = {};
    myLib.myFunc1 = function() {
        //Do some cool stuff
    };
    return myLib;        
};    

if (typeof module !== 'undefined') {
    module.exports = myLibrary();
} else{
    var app = angular.module('myApp.services', []);
    app.factory('myLibrary',myLibrary);
}

This will work, but it has at least two significant limitations:

  • The namespace is polluted with the variable "myLibrary"
  • If within the library I want to use other node modules or Angular services then I can't

I would like to know if anyone knows of a better solution, or if I should give up on using Angular services and use one of the libraries that allows client side Node module functionality.

1 Answer 1

1

Having done a bit of searching about this looks like the best solution:

https://gist.github.com/sevcsik/9207267

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

Comments

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.