I have an npm module that I would like to configure once and call in multiple places.
The npm module (let's call it 'signature') is basically like this
module.exports = function(options) {
return new Signature(options);
};
var Signature = function(options) { }
Signature.prototype.sign = function() {}
I made another module ('signer') to configure it:
var signature = require('signature');
module.exports = function() {
// I pass whatever config options here
return signature({});
};
In my code I do:
var signer = require('../utils/signer');
signer.sign();
However this gives me a "has no method "sign" error. What am I doing wrong? I suspect I have to initialize something but not sure what. If I bypass the config module (signer) and just call the signature module then it works fine:
var signature = require('signature');
var s = signature();
s.sign();
Signer exports a function that returns a signature. Try:
var signer = require('../utils/signer');
signer().sign();
Related
I am java, c# programmer with more than 10 years experience. Now I have to write some code in js and really, I feel like Alice in Wonderland....
I have myModule.js :
module.exports = function (logger) {
return {
read: function (param) {
......
logger.eror(....)
},
write: function (param) {
......
logger.info(....)
}
};
}
The logger.js, (legacy code, I cannot change it) looks similar:
module.exports = function(some_settings){
return {
info: function () {
......
},
error: function () {
......
}
};
}
Important thing: logger is not included to myModule by require("logger") but injected in some "global" index.js file which looks like:
var settings = require("../someSettings.js")();
var logger = require("../logger.js")(settings);
var myModule = require("../myModule .js")(logger);
Now I want to write UT for myModule in mocha. So I want to mock logger. And I face the wall.
I tried proxyquire but this is not good solutions because logger is not included to myModule by require.
I tried sinon stub and mock e.g.:
var logger = require("logger.js");
var loggerMock = sinon.mock(logger);
var myModule= require("myModule.js")(loggerMock);
Or:
var logger = require("logger.js");
var loggerStub = sinon.stub(logger, "info", function() {return ....});
var myModule= require("myModule.js")(loggerStub);
But I still have some errors like :
"Attempted to wrap undefined property error as function"...
Please, be my white rabbit in this javascript freeky world...
When you are testing your module, you can use a fake object for your logger object, i.e.:
const logger = {info: noop, error: noop, debug:...};
function noop() {}
or well create a mock object with sinonjs if you prefer create it programatically and use spy features.
Then, you can inject it wrapping your module definition in a closure (as you posted):
# myModule.js:
module.exports = (dep1, dep2, ...) => {
// module definition
}
Or use rewire package, to "inject" module scoped dependencies (it works only for local-vars, and not need to create a closure in your module):
# myModuleTest
myModule = rewire('./myModule');
myModule.__set__('logger', { /* fake logger */ });
I am wondering how I can augument a commonjs module from another module that requires it.
Let's assume that I have three files, two commonjs modules as below:
my-example-module.js
function MyExampleModule(){}
MyExampleModule.prototype = {
bindings: {
some: 'random',
prop: 'values'
}
}
module.exports = MyExampleModule;
another-example-module.js
var MyExampleModule = require('./my-example-module');
function AnotherExampleModule(){}
AnotherExampleModule.prototype = {
getMyExampleModuleBindings: function(){
var module = new MyExampleModule();
return module.bindings;
}
}
module.exports = AnotherExampleModule;
app.js
var MyExampleModule = require('./my-example-module');
var AnotherExampleModule = require('./another-example-module');
//modify?!?
var anotherExampleModule = new AnotherExampleModule();
console.log(anotherExampleModule.getMyExampleModuleBindings());
So what I want to do is have //modify?!? be some kind of code that will alter the original MyExampleModule prototype so when anything else attempts to require MyExampleModule it will get the modified version.
A concrete question would be - what should I replace //modify?!? with so I get logged out the the console with the assumption that my-example-module.js is read only.
{
some: 'random',
prop: 'values',
added: 'binding'
}
If you want to do this in nodejs/iojs, this is pretty simple. When node imports a CommonJS module (let's call it A), it creates an internal object.
When another module (B) wants to load the same module (A), it just gets the reference to that internal object. So if you change something on MyExampleModule in app.js it is also applied to the MyExampleModule in another-example-module.js:
app.js
var MyExampleModule = require('./my-example-module');
var AnotherExampleModule = require('./another-example-module');
//modify:
MyExampleModule.prototype.bindings = {
some: 'random',
prop: 'values',
added: 'binding'
};
var anotherExampleModule = new AnotherExampleModule();
console.log(anotherExampleModule.getMyExampleModuleBindings());
Since you create a new instance of MyExampleModule in another-example-module.js after you call MyExampleModule.prototype.bindings = {...} in app.js, the new instance will already be created with the modified .prototype.
While I haven't tested this in browserify, it certainly works in webpacks implementation of CommonJS as well.
Check out the working example on runnable (app.js is called server.js):
http://code.runnable.com/VZPdN5k65gE5vUIz
I have read a lot of articles about how to create modules in node.js, and you can use module.exports to expose module internals to the file which includes it.. awesome!
How does this work the other way around? I'll use the following as an example :-)
USER.JS
function User() {
this.property = 'value';
this.doSomething = function() {
var getStuff = mainFileFunction();
// do something with getStuff
}
module.exports = User;
MAIN.JS
var myArray = [];
myArray.push('something');
myArray.push('something else');
mainFileFunction() {
for(thing in myArray) {
return myArray[thing];
}
}
var u = new user();
log(u.property); <--- THIS IS EXPOSED, COOL!
u.doSomething(); <--- This will throw an error because mainFileFunction is not defined in user.js :-(
If I were to move mainFileFunction to the user file, then it still wouldn't work because the myArray array wouldn't be defined... and if I were to move that over too, I wouldn't be able to use it in other functions in main (which I want to) :-)
I'm sorry if I'm missing something really obvious here... What I want is to expose the parts of my choosing from modules I include (module.export works for that) but I also want to expose everything from the main file to all the includes..
or just expose everything to everything? is that totally messy and horrible??
Just to explain what I am trying to do here... I want to have classes defined in separate files, but I want to instantiate a bunch of them as objects in the main file and store them in arrays.. I want the objects to contain methods which can access arrays of the other object types.
Thanks guys! :-)
You can use globals, or have a proper circular dependency (requireing both files), however - this is usually a bad habit which can lead to maintainability problems in the future.
Instead, you can use dependency injection and inject doSomething into your module.
This basically gives you the following for free:
You can test User with a simple mock implementation of doSomething later and verify the correctness of your code
The dependencies of a user are explicit and not implicit, which makes it obvious what a user needs.
I'll provide two implementations, one using constructor dependency injection and one with a module wide setting.
USER.JS
function User(dependentFunction) {
this.property = 'value';
this.doSomething = function() {
var getStuff = dependentFunction();
// do something with getStuff
}
}
module.exports = User;
MAIN.JS
...
var u = new User(mainFileFunction);
u.doSomething(); // this will now work, using mainFileFunction
What happens here is fairly simple, and we know what's going on.
This can also be a module wide setting
USER.JS
function User(depFunc) {
this.property = 'value';
this.doSomething = function() {
var getStuff = depFunc();
// do something with getStuff
}
}
function UserFactory(depFunc){
return function(){
return new User(depFunc);
}
}
module.exports = UserFactory;
MAIN.JS
var getUser = UserFactory(mainFileFunction);
var u = getUser(); // will return a new user with the right function
+1 to Benjamin answer for dependency injection.
I would like to add another way to inject objects in your modules by passing the dependency in the require like require('./module.js')(dependentFunction);
//MAIN.js
var db = function() {
this.rows = [];
this.insert = function(name) {
this.rows.push(name);
console.log('Db: Inserting user with name ' + name);
}
this.getAll = function(){
return this.rows;
}
}
var fakeDb = new db();
var user = require('./user.js')(fakeDb);
user.add('Jhon');
user.add('Rose');
user.list();
and
//users.js
module.exports = function(db) {
return {
add: function(name) {
db.insert(name);
},
list: function() {
var users = db.getAll();
var i = users.length;
console.log('listing users\n-------');
while(i--) {
console.log(users[i]);
}
}
}
}
You should pass mainFileFunction as a parameter to the constructor of user.
USER.JS
function User(mainFileFunction) {
this.property = 'value';
this.doSomething = function() {
var getStuff = mainFileFunction();
// do something with getStuff
}
module.exports = User;
In your main.js use the following
var u = new user(mainFileFunction);
How about moving mainFileFunction to user.js, and have the function accept an array as an argument:
mainFileFunction(array) {
for(thing in array) {
return array[thing];
}
}
And then when you call it from main.js, pass the function your array:
u.doSomething(myArray);
When I do:
lib = require('lib.js')(app)
is app actually geting passed in?
in lib.js:
exports = module.exports = function(app){}
Seems like no, since when I try to do more than just (app) and instead do:
lib = require('lib.js')(app, param2)
And:
exports = module.exports = function(app, param2){}
I don't get params2.
I've tried to debug by doing:
params = {}
params.app = app
params.param2 = "test"
lib = require("lib.js")(params)
but in lib.js when I try to JSON.stringify I get this error:
"DEBUG: TypeError: Converting circular structure to JSON"
When you call lib = require("lib.js")(params)
You're actually calling lib.js with one parameter containing two properties name app and param2
You either want
// somefile
require("lib.js")(params);
// lib.js
module.exports = function(options) {
var app = options.app;
var param2 = options.param2;
};
or
// somefile
require("lib.js")(app, param2)
// lib.js
module.exports = function(app, param2) { }
You may have an undefined value that you're trying to pass in.
Take for instance, requires.js:
module.exports = exports = function() {
console.log('arguments: %j\n', arguments);
};
When you call it correctly, it works:
node
> var requires = require('./requires')(0,1,2,3,4,5);
arguments: {"0":0,"1":1,"2":2,"3":3,"4":4,"5":5}
If you have a syntax error, it fails:
> var requires = require('./requires')(0,);
... var requires = require('./requires')(0,2);
...
If you have an undefined object, it doesn't work:
> var requires = require('./requires')(0, undefined);
arguments: {"0":0}
So, I'd first check to see that your object is defined properly (and spelled properly when you pass it in), then check that you don't have syntax errors.
I'm writing a simple server for Node.js and I'm using my own class called User which looks like:
function User(socket) {
this.socket = socket;
this.nickname = null;
/* ... just the typical source code like functions, variables and bugs ... */
this.write = function(object) {
this.socket.write(JSON.stringify(object));
}
};
and then later in the process I'm instantiating it a lot:
var server = net.createServer(function (socket) {
/* other bugs */
var user = new User(socket);
/* more bugs and bad practise */
});
Can I move my User class definition to another javascript file and "include" it somehow?
You can simply do this:
user.js
class User {
//...
}
module.exports = User // 👈 Export class
server.js
const User = require('./user.js')
let user = new User()
This is called CommonJS module.
ES Modules
Since Node.js version 14 it's possible to use ES Modules with CommonJS. Read more about it in the ESM documentation.
user.mjs (👈 extension is important)
export default class User {}
server.mjs
import User from './user.mjs'
let user = new User()
Using ES6, you can have user.js:
export default class User {
constructor() {
...
}
}
And then use it in server.js
const User = require('./user.js').default;
const user = new User();
Modify your class definition to read like this:
exports.User = function (socket) {
...
};
Then rename the file to user.js. Assuming it's in the root directory of your main script, you can include it like this:
var user = require('./user');
var someUser = new user.User();
That's the quick and dirty version. Read about CommonJS Modules if you'd like to learn more.
Another way in addition to the ones provided here for ES6
module.exports = class TEST{
constructor(size) {
this.map = new MAp();
this.size = size;
}
get(key) {
return this.map.get(key);
}
length() {
return this.map.size;
}
}
and include the same as
var TEST= require('./TEST');
var test = new TEST(1);
If you append this to user.js:
exports.User = User;
then in server.js you can do:
var userFile = require('./user.js');
var User = userFile.User;
http://nodejs.org/docs/v0.4.10/api/globals.html#require
Another way is:
global.User = User;
then this would be enough in server.js:
require('./user.js');