2

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

The include part worked. But hashing is still not able to use encoding function.
Ok. I added the require encoding part in hashing.js and it worked like charm. Thanks.:)

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.