How to make object references available in Node modules? - javascript

I have an Express.js web app. In the main app.js file, I require() a bunch of third party dependencies. Recently I've started extracting parts of my app.js code into separate modules, e.g.
// in app.js
var users = require('./modules/users');
// and then e.g.
// users.add(doc);
Inside my modules, I sometimes need to use objects that are available in my main app.js file and I'm wondering what's the best way to pass references to those object to my modules.
My current approach:
// in app.js
// pass needed object references in initialization step
users.init([db, logger]);
and
// in modules/users.js
var db, logger;
exports.init = function (deps) {
db = deps[0];
logger = deps[1];
};
Does this approach make sense? Is there a better way to perform this?

Sure, just use modules! :)
// db.js
// create db instance here rather than in app.js
module.exports = db;
And
// logger.js
// create logger instance here rather than in app.js
module.exports = logger;
Then
// app.js
var db = require('./db');
And
// lib/somewhere.js
var db = require('../db');
This way you're able to rely on the CommonJS dependency injection system rather than on doing the dependency injection all by yourself (passing references around instead of requireing a module into yours).
The reason why this works as expected is that modules are only interpreted once, so if you instantiate everything once as opposed to using a factory function, it just works.

You should just be able to require modules as normal:
// users.js
var db = require('./db');
exports.init = function() {
// use db in here
};
However, sometimes this isn't possible and you will need to explicitly pass in the module.
One way to do it is to pass in dependencies when you require the module:
// users.js
module.exports = function(db, logger) {
return {
init: function() { /* use db and logger in here */}
};
}
// app.js
var db = ...;
var logger = ...;
var users = require('./users')(db, logger);
users.init();
This is the pattern that I personally prefer, I think it's cleaner to pass dependencies into the require than into some init method like you have in your example.
You'll see this done in ExpressJS code quite a lot, for example when we have all our routes in another file and need to pass our app instance around:
require('./routes')(app);

If you need something to be initialized specifically in app.js rather than their own module you can export them from app.js and then require app.js:
// app.js
var db = require("db"),
logger = require("logger");
// do your initialization with db and logger
module.exports = { db: db, logger: logger };
and then:
// users.js
var db = require("./app").db,
logger = require("./app").logger;
// use db and logger
This works because as #Nico mentioned, modules are interpreted once and app.js will not be interpreted each time it is required from elsewhere.

Related

How to export an object with methods and properties

I have two js files in Electron (which uses Nodejs) and I try to export from one and require in another.
app.js:
App = {
server: {
host: '192.168.0.5',
user: 'root',
}
ping: function() {
}
}
exports.App = App
I have tried every way possible of exporting, including module.exports = App, module.exports.App = App and so on.
ping.js first attempt:
var App = require('../app.js') // I have also tried adding .App to the end
console.log(App) // This returns an object which contains the App object
ping.js second attempt:
var App = require('../app.js')
App.x = 'y'
console.log(App) // this returns an object which contains the App object and the x property
It may appear that App contains another App object, but console.log(App.App) says it doesn't exist.
The first thing I'd to do solve this would be to make sure I'm using the full path to the required module, as in:
const Path = require('path')
const App = require(Path.join(__dirname,'../app')) // the .js isn't needed here.
Note that this assumes that the app.js file is in the immediate parent directory of the one in which the application runs.
If that doesn't work, I'd make sure the files are where you think they are, and that the process you're running is located within the file system where you think it is. You can determine this by adding this to the top of your main script file:
console.log("current working directory:",process.cwd())
Or in es6:
console.log(`current working directory: %s`, process.cwd())
If the printed directory doesn't match your assumptions, modify your require statement accordingly.
And for the record, the "correct" way to export your App map would be to:
const App = {
...
}
module.exports = App
Or using es7:
export default App = {
...
}
(See export for more on es7 modules.)
Either way, you'd then require the module as:
const App = require(PATH_TO_APP)

Avoid to repeat require statements in nodejs modules

Currently I'm develloping an app in Express and I've created several services which are modules called from a single index.js file:
"use strict";
const MAX_TEXT_LENGTH = 100;
var mysql = require('mysql')
, _ = require('underscore')
, Q = require('q')
, moment = require('moment')
, env = process.env.NODE_ENV || 'development'
module.exports.module1 = require('./module1');
module.exports.module2 = require('./module2');
module.exports.moduleN = require('./moduleN');
Thanks to this structure I can now call a single time to var Services = require('./services') and I've access to all services.
The problem is that the modules, constants and "global" vars used in this file aren't available in the modules so I have to call them many times.
I've also tried to name vars as let but this doesn't work.
Is there any way to be able to define modules and variables in this index.js file and use them inside modules?
Thanks!!!
In your modules, instead of returning only one simple value, return an object where each key is one the variables that you want to export. Something like this.
var myModuule = {
varIWantToExport: 1,
methodIWantToExport: function() { return "Hello world!";}
}
module.exports = myModule;
And then, when you require it in other modules, you can do something like this:
var myModule = require("./my-module");
var myMethod = myModule.methodIWantToExport
var myVar = myModule.varIWantToExport;
Obviously this only applies to your modules. If you are trying to use some variables/methods that are not publicly exposed in a module, maybe you're doing something wrong.

Listing out the dependancies from a file based on what's required in the file

If I have a file called index.js and it requires a couple of modules. Is there any way I can get a list of the modules that are required?
var _ = require("underscore")
var express = require("express")
var app = express()
return app
Then I have another file like this.
var listRequired = require("list-required")
listRequired("./index.js").then(function(moduleTypes){
console.log(moduleTypes.npm) // ["underscore", "express"]
})
I'd really also like a way to track npm modules (ex. underscore), native node modules (ex. crypto, path) and local files (ex. ./routes.js)
So if index.js contained this:
var _ = require("underscore")
var express = require("express")
var routes = require("./routes")
var app = express()
module.exports = app
And routes.js contained this:
var crypto = require("crypto")
var Promise = require("bluebird")
module.exports = {}
Then I have another file like this.
var listRequired = require("list-required")/*(file, deepOption)*/
listRequired("./index.js", true).then(function(moduleTypes){
console.log(moduleTypes.native) // ["crypto"]
console.log(moduleTypes.local) // ["./routes"]
console.log(moduleTypes.npm) // ["underscore", "express"]
})
Does this already exist in some capacity? ES6 import support would be nice as well.
The technique used to solve this kind of problem is called static analysis. You use something that parses source code and returns an abstract syntax tree. (This is very much like how a browser reads HTML and turns it into a DOM.)
Once you have an AST, it is relatively straightforward to walk the tree and look for interesting bits like require calls.
It looks like someone has already written a module that does exactly that, called required.

var express = require('express'); var app = express(), What is express()?? is it a method or a constructor? Where does it come from

var express = require('express');
var app = express();
This is how we create an express application. But what is this 'express()'? Is it a method or a constructor? Where does it come from??
Is it a method or a constructor?
Neither; it's a function, although if you said "method" I don't think anyone would give you a hard time.
A method is a function attached to an object. In JavaScript, methods are just mostly functions that you reference via object properties. (Update: As of ES2015, if you use method syntax to create them, they're slightly more than that because they have access to super.)
A constructor, in JavaScript, is a function you call via the new operator. Even though other functions may create things, we don't typically call them "constructors" to avoid confusion. Sometimes they may be "creator" or "builder" functions.
Where does it come from?
ExpressJS is a NodeJS module; express is the name of the module, and also the name we typically give to the variable we use to refer to its main function in code such as what you quoted. NodeJS provides the require function, whose job is to load modules and give you access to their exports. (You don't have to call the variable express, you can do var foo = require('express'); and use foo instead, but convention is that you'd use the module's name, or if only using one part of a module, to use the name of that part as defined by the module's documentation.)
The default export of express is a bit unusual in that it's a function that also has properties on it that are also functions (methods). That's perfectly valid in JavaScript,¹ but fairly unusual in some other languages. That's why you can create an Application object via express(), but also use express.static(/*...*/) to set up serving static files.
¹ In fact, it's completely normal. Functions have a couple of standard methods by default: call, apply, and toString for instance.
You’ll use Node’s require function to use the express module. require is similar to keywords like import or include in other languages. require takes the name of a package as a string argument and returns a package. There’s nothing special about the object that’s returned—it’s often an object, but it could be a function or a string or a number.
var express = require('express');
=> Requires the Express module just as you require other modules and and puts it in a variable.
var app = express();
=> Calls the express function "express()" and puts new Express application inside the app variable (to start a new Express application).
It's something like you are creating an object of a class. Where "express()" is just like class and app is it's newly created object.
By looking the code of express below you are good to go what is really happening inside.
File 1: index.js
'use strict';
module.exports = require('./lib/express');
File 2 : lib/express.js
'use strict';
var EventEmitter = require('events').EventEmitter;
var mixin = require('merge-descriptors');
var proto = require('./application');
var Route = require('./router/route');
var Router = require('./router');
var req = require('./request');
var res = require('./response');
/**
* Expose `createApplication()`.
*/
exports = module.exports = createApplication;
function createApplication() {
var app = function(req, res, next) {
app.handle(req, res, next);
};
mixin(app, EventEmitter.prototype, false);
mixin(app, proto, false);
app.request = { __proto__: req, app: app };
app.response = { __proto__: res, app: app };
app.init();
return app;
}
exports.application = proto;
exports.request = req;
exports.response = res;
exports.Route = Route;
exports.Router = Router;
});
How require works
When you call require('some_module') in node here is what happens:
if a file called some_module.js exists in the current folder node will load that, otherwise:
Node looks in the current folder for a node_modules folder with a some_module folder in it.
If it doesn't find it, it will go up one folder and repeat step 2
This cycle repeats until node reaches the root folder of the filesystem, at which point it will then check any global module folders (e.g. /usr/local/node_modules on Mac OS) and if it still doesn't find some_module it will throw an exception.
Ancient post. I think the original poster was confused about why the syntax to call the function exported by module express is
var app = express()
instead of
var app = express.express()
To clarify: require() function does not create a reference to that 'module'. There's no such thing as reference to a module. There's only reference to thing(s) exported by a module.
require('xxx.js'), where the .js extension can be omitted, returns whatever is exported by that xxx.js file. If that xxx.js file exports an object, require('xxx.js') returns an object; if a function is exported, require('xxx.js') returns a function; if a single string is exported, require('xxx.js') returns a string...
If you check source code of file express.js, you will see that it exports a single function. So in
var express = require('express')
The first express is assigned whatever is exported by module express, which in this case happens to be a single function. express is a function, not a reference to a module. Hence on second row you just invoke that function:
var app = express()
Hope this helps!
let me answer this question by an example.
create 2 javascript files.
play1.js and express.js
//express.js
function createApplication(){
var app = 'app';
return app;
}
module.exports = createApplication;
//keep in mind that we are not doing module.exports = {createApplication}
now import express.js in play1.js file
//play1.js
var express = require('./express);
var app = express();
// this will call createApplication function as app is referencing to it.
console.log(app); // "app"
Whenever you import a module like
const express = require('express')
express is a module with functions or objects or variables assigned to it .
take a look at /lib/express
you are able to access the function createApplication inside express module as express() because the function is assigned directly to the module like
exports = module.exports = createApplication;
function createApplication(){
var app = function(req, res, next) {
app.handle(req, res, next);
};
//other codes
}
so you are able to access the function createApplication just calling express() as function
now when you check out the other section of the express library, you can see a bunch of other objects attached to the exports special object as well.
/**
* Expose the prototypes.
*/
exports.application = proto;
exports.request = req;
exports.response = res;
/**
* Expose constructors.
*/
exports.Route = Route;
exports.Router = Router;
// other exports
these objects or function assigned to export special object can be accessed from the import section using express as an object.
express.{name}
express.Route
express.Router etc
In the end you are just exporting a bunch of methods or objects that are attached to the module.export special object inside express js file
to read more on module.export special object go here
1- var express = require('express');
first line require the express package .js file, und "require" it's only returnd what was exported in the js file with (module.exports).
so we have only pointer to this function .
2- var app = express();
in second line, we use 'app' as pleaceholder to receive the output from express() function, which is an object, we can use it in our code (by accessing his methods and properties like any other Class )
in other words, we use the 'app' Object, which which produced from 'express()' function, that we imported from 'express.js' file .
NOTE 1) and of course we can give any name instead of 'app' , but it's a good practice when you follow what the most developers use to name this packages, that make easier to understand your code specialty when you work in team.
NOTE 2) after ES6, we use 'const' instead of 'var' .
Simple what we wrote in node js when we require a modules for our application
const modue_need1=require('module_name');
const modue_need2=require('module_name2');
const modue_need3=require('module_name3');
const modue_need4=require('module_name4');
const modue_need....=require('module_name.....');
So for every module, we need to write such a big code, and time-consuming now to reduce these lengthy codes and time slice what we do? We need node js Framework like Express js
which will overcome these problems mean "write less, do more"
we just use this two-line and all the requirement(modules) about our app will be their in-app object which we can use whenever we need so do not need to call require for every module.
"write less, do more"
const express=require('express');
const app=express();
console.log(app);

Node.js, Express and Dependency Injection

I'm in early stages of a node.js project, and I'm looking to improve the overall app organization. In the past I worked with Symfony2 (PHP) and now I code a lot in Angular, both of which relly heavily on DI. So, I really like the idea of applying the same principles in my node.js project.
I know the existence of packages like Rewire, but for now I'll like to try the DI approach. The issue is, how to achieve an equilibrium to keep the lightweight feeling that gives working with node with the solidity of a well tested dependency injected app (I know that well tested is what gives the solidity ;-)).
Node modules
One of the issues, would be how to manage the external modules, what to do if some object needs fs module? As Vojta Jina (from AngularJS) states in this article:
So the best way that works for me right now is something like this: Modules are stateless. They only contain definitions of classes/functions/constants.
So, I suppose that I would have to inject everything:
function Foo(fs) {
this.fs = fs;
}
Foo.prototype.doSomething: function () {
// this.fs...
};
module.exports = Foo;
Somewhere:
var fs = require('fs');
var Foo = require('./Foo');
var foo = new Foo(fs);
foo.doSomething();
Express
Since Express uses apply() to call the handlers the context is lost and we can't use this. So we're left with these:
// foo.js
function Foo(fs) {
this.fs = fs;
}
Foo.prototype.index = function () {
var self = this;
return function (req, res, next) {
// self.fs...
};
};
module.exports = Foo;
// bar.js
module.exports.index = function (fs) {
return function (req, res, next) {
// fs...
};
};
// app.js
var express = require('express');
var fs = require('fs');
var app = express();
var Foo = require('./foo');
var foo = new Foo(fs);
var bar = require('./bar');
app.get('/foo', foo.index());
app.get('/bar', bar.index(fs));
So...
Has someone taken this approach? What about the use of DI frameworks? (like di.js) And how to keep the experience lean? All ideas are welcome. Thanks!
You have some good thoughts to which I'd like to add:
Having stateless modules will help you to scale your app horizontally. If all state is in a database it will be easy to run multiple node.js instances in parallel.
I also prefer to inject everything. Otherwise the time will come when I would like to write a unit test and it gets hard because I have a hardcoded (not injected) dependencies I can't mock.
To keep this lightweight feeling when working with node you need an approach for dependency injection that doesn't add too much complexity. Your express example above reminds me of a talk by Vojta Jina in which he makes an important point about the wiring part of dependency injection. (Watch minute 3:35 to 8:05) I can't explain it any better than Vojtja does in his talk but basically he says that we need a di framework that takes care of the wiring (what is injected into what). Otherwise the code we manually write to set up the wiring won't be maintainable. Also each unit test would need such wiring code. And that IMO is where manual dependency injection is not an option anymore.
When you use a di framework (or a di container as many people say) the basic idea is that each individual module states which dependencies it requires and through which id it can be required by other modules. Then the di framework can be invoked to initialize the module that serves as an entry point (e.g. app.js) and the framework will look up all dependencies and takes over the hard work of injecting the appropriate module instances.
There are many di frameworks for node.js to which I'd like to add my own: "Fire Up!" If you would use it your example would look like this:
foo.js
// Fire me up!
function Foo(fs) {
this.fs = fs;
}
Foo.prototype.index = function () {
var self = this;
return function (req, res, next) {
// self.fs...
};
};
module.exports = {
implements: 'foo',
inject: ['require(fs)'],
_constructor: Foo
};
bar.js
// Fire me up!
module.exports = {
implements: 'bar',
inject: ['require(fs)'],
factory: function (fs) {
return {
index: function (req, res, next) {
// fs...
}
};
}
};
app.js
// Fire me up!
module.exports = {
implements: 'app',
inject: ['require(express)', 'foo', 'bar']
};
module.exports.factory = function (express, foo, bar) {
var app = express();
app.get('/foo', foo.index());
app.get('/bar', bar.index);
};
index.js
var fireUpLib = require('fire-up');
var fireUp = fireUpLib.newInjector({
basePath: __dirname,
modules: ['./lib/**/*.js'] // foo.js, bar.js, app.js are on this path
});
fireUp('app'); // This is where the injection is kicked off.
When running node index.js you get the following output:
fireUp# INFO Requested: app, implemented in: lib/app.js
fireUp# INFO |-- Requested: require(express)
fireUp# INFO |-- Requested: foo, implemented in: lib/foo.js
fireUp# INFO |-- Requested: require(fs)
fireUp# INFO |-- Requested: bar, implemented in: lib/bar.js
fireUp# INFO |-- Requested: require(fs)
If this looks worth trying out you might be interested in the Getting Started section which shows an example based on express.
Hope that helps!
You can check https://www.npmjs.com/package/plus.container
This is close to DIC in Symfony

Categories

Resources