1

I have come across a few modules i would like to use in my Angular app but am at a crossroads on how to make work in my angular app as i will need to "require()" in my factory file.

Heres the node module im interested in: https://github.com/TimNZ/node-xero

On this current project i am using Gulp Angular in Yeoman to generate my boilerplate and am having a hard time figuring out how i should make this work if i need to modify any of the gulp scrips.

I was thinking i can just "Browserify" the single file that will use require() but is this the wrong approach? should i just browserify all the project files? is this standard practice?

Any advice is appreciated, currently has me at a stand still.

All the modules i want to use in relation to Xero all seem to be node modules.

2 Answers 2

2

The simplest starting point would be to use Browserify to build a standalone bundle that uses a global of your choice.

To do this, you could create a JS file that requires the node module(s) you want to use. You could create a file named bundle-index.js with this content:

exports.xero = require('node-xero');

You could then run this command to build a standalone module:

browserify --standalone the_global bundle-index.js > bundle.js

Where the_global is a name you find appropriate for the global object that will contain the exports. With the bundle.js file included in a script element, you would be able use it like this:

var privateApp = new window.the_global.xero.PrivateApplication({ ... });

Doing things this way would involve the least amount of disruption to your current project. And if you are only needing to use Browserify to require third party libraries that don't change frequently, you could start with a simple manual process for building the standalone bundle.

Note that you can export other required modules by adding additional exports to bundle.js:

exports.xero = require('node-xero');
exports.someOtherModule = require('some-other-module');
Sign up to request clarification or add additional context in comments.

Comments

0
(function(){

   var xero = require('node-xero');

    angular
        .module('app')
        .factory('myFactory', myFactory);

    function myFactory(){

        var helper = {
            myMethod: myMethod,
        };

        return helper;

        function myMethod(){
            xero.doStuff();
        }

    }
})();

1 Comment

Being a browser specific app, "require()" isn't usable.

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.