0

I'm new to building custom npm packages and I'm getting lost configuring it with data coming from the application it is using it.

EDIT: This is just an example but the app will have more methods and those fake a and b will be used from many of those methods.

Basically on App I'm requiring my package in this way:

var a = 'a';
var b = 'b';
var module = require('module')(a, b);
module.test();

My module in his index file has:

var a;
var b;

function test() {
  return {
    a: a,
    b: b
  };
}

module.exports = function(_a, _b) {
  a = _a;
  b = _b;

  return {
    test: test
  }
};

As you can guess it is not working as I was expecting... How can I pass my custom data to my npm package and be able using that data along my methods?

Ayeye Brazo
  • 3,316
  • 7
  • 34
  • 67
  • 1
    This should work as you expected and it does on my machine - did you try to use the name `module` for your module or it just an example? If so, try renaming it to something else as "module" is a predefined module? – Joschi Nov 16 '17 at 14:52
  • It look like it doesn't receive those 2 variables... – Ayeye Brazo Nov 16 '17 at 14:54
  • 1
    The code you posted here has no problem. May be your original code have issues. – wrangler Nov 16 '17 at 14:55
  • If you guys have no issues maybe you're right and there is another bug in the app. Thanks for the hint and checks, I will take a deeper look. – Ayeye Brazo Nov 16 '17 at 14:58
  • Other thing no need to return test function. `module.test()` will do what you are intending to do. just include `module.exports.test = function (){ }` also – wrangler Nov 16 '17 at 15:00

2 Answers2

3

shouldnt you use it something like this

var a = 'a';
var b = 'b';
var module = require('module');
module.init(a,b);

// do some other code....

module.test();

and in your module like this:

var _a = null;
var _b = null;
var test = function() {
    return {
        a: _a,
        b: _b
    }
}

var init = function(a, b) {
    _a = a;
    _b = b;
}

module.exports = {
    init,
    test
};
  • no because I will have several methods using those configuration and I can not pass those configuration every time. I wish to do it at the beginning following the pattern: `require('module')(data);` – Ayeye Brazo Nov 16 '17 at 14:47
  • maybe like this, updated? its untested tho, just from mind :) – Lucas Reppe Welander Nov 16 '17 at 15:06
0

Your definition of a module is absolutely fine but it depends on how you plan on using it!

If you would like to use it as though it were published on the npm-registry then you need to use the process described here: Installing a local module using npm?

I know that this is an example but for other readers - you shouldn't use the name module as this is already in use by Node.

If you are simply using a modular approach to producing a larger app and you want to use the exports of your file, then you require the file by pointing to it. For example, if this file was called module.js and is in the same directory as the script which is requiring it then use:

  var myModule = require('./module.js')(a, b);

If you have it in another directory then use the normal relative path navigation syntax like ../module.js if it is up one directory or ./lib/module.js if it is in a sub-directory called lib

John
  • 1,313
  • 9
  • 21