Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: calling pure Javascript functions

I am using Nodejs for writing some sample programs. I am facing an issue with calling Javascript files from within Nodejs. Say I have 3 .js files: A.js, B.js, C.js. A.js is written in Node. B.js and C.js are written in pure Javascript. Now i need to call a function b() present in B.js from A.js. So I eval() B.js and export the method b(). This works properly. But the issue is when my function b() in B.js calls a function c() in C.js.

B.js:

function b()
{
    console.log('In function b');
    c();
}

C.js:

function c()
{
    console.log('In function c');
}

Just to add on to the question. I have a var in B.js: var abc, in the global space of B.js. In C.js I can reference to it as just: var a = abc; How to make sure my C.js can have access to my variable abc?

How do i make sure the dependancy is resolved? Any help would be appreciated. Thanks!!!

like image 713
user1999645 Avatar asked Oct 31 '25 17:10

user1999645


1 Answers

You should use modules in Node.js. It very simple, just read the docs.

B.js

var c = require('./C');

function b() {
    console.log('In function b');
    c();
}

C.js

module.exports = function() {
    console.log('In function c');
}
like image 83
NiLL Avatar answered Nov 03 '25 09:11

NiLL