skjindal93
skjindal93

Reputation: 734

Include Dependent JS files in Node JS

I figured out how to include a JS file in Node JS. But I have a JS file(hashing.js) which uses a function from other JS file(encoding.js).

In encoding.js, I have

exports = exports || {};
exports.encoding = encoding function

In hashing.js, I have

exports = exports || {};
exports.hashing = hashing function

The hashing function uses encoding inside it.

I am including them in Node JS like

var encoding = require (./encoding.js);
var hashing = require (./hashing.js);

But when I am including the JS files like this, running the hashing var throws an error

encoding is not defined

So I am unable to include JS files in Node JS which depend on some other JS files.

Upvotes: 2

Views: 516

Answers (1)

mpm
mpm

Reputation: 20155

dont do that

exports = exports || {};
exports.encoding = encoding function

do that

module.exports = function(){}

or

exports.encoding = function(){}

then

 var encoding = require (./encoding).encoding;

I suggest you take some time to read :

http://nodejs.org/api/modules.html

Upvotes: 1

Related Questions