0

I am writing some logic in fileB.js that needs to call another function declared in fileA.js. fileA.js declares a function called abc(). How can I call abc() from fileB.js.

Both fileA and fileB are in the same directory.

Thank you

Ps. I am not using this with HTML. I am just running it locally as part of another project I am working on. I am now using node to run it from terminal. But I am not using anything else in these two files. I just have a bunch of really simple functions. No node.js modules or anything...

4
  • 1
    Use Requirejs, it supports multiple environments and doesn't require a browser. Commented Oct 25, 2014 at 19:12
  • Are you using node.js? Commented Oct 25, 2014 at 19:13
  • How exactly are you running fileA? Commented Oct 25, 2014 at 19:13
  • so for now I am running it using "node fileB.js".... Commented Oct 25, 2014 at 19:15

4 Answers 4

2

Node.js isolates each file as a module, giving each their own local scope to define essentially "private" functions and vars.

With this isolation, fileA will need to export abc in order to share it with other files/modules:

function abc() {
    // ...
}

exports.abc = abc;

Then, fileB can require() fileA with a relative path (. referring to the current directory) and use its exported function:

var a = require('./fileA');

a.abc();
Sign up to request clarification or add additional context in comments.

1 Comment

I am marking this as the correct not because I know the other ones do not work. I marked it as the answer because this worked for me and it seemed the most straight forward to implement at first look. Thank you.
1

If you are using node, then in fileA:

module.exports = { abc: abc } //assuming that abc holds a reference to your function, declared somewhere above

Then, in fileB you require fileA and use what you exported:

var fileA = require('./fileA.js');
fileA.abc();

Comments

1

Since you're running it in NodeJS, I'd suggest doing the following at the top of fileB.js:

var fileA = require( './fileA.js' );

However, in order for this to work, the functions you want to use in fileB.js should be exported from fileA.js. To do this, lets assume that the function abc() is what you want to access:

// In fileA.js:
function abc() {
    // ... do something ...
}

module.exports = abc;

If you want multiple functions/variables/objects available to fileB.js, you can export them all as one object:

// In fileA.js
function abc() {
    // ... do something here ...
}

var myObject = {
    propOne: 'foo',
    propTwo: 'bar
};

module.exports = {
    abc: abc,
    myObject: myObject
};

Then, within fileB.js:

// Import the fileA object you just created.
var fileA = require( './fileA.js' );

// Save references to abc and myObject
var myObject = fileA.myObject,
    abc = fileA.abc;

Comments

0

Just include both the JS files in the HTML and then simply call them any function anywhere, it will work.

5 Comments

I am not using the JavaScript in an HTML page. I'm just running it locally for other stuff I'm working on
What do you mean you are running it locally?
I'm running it from terminal.
@zumzum be more specific please. There are many ways to run JS from terminal.
I am using node... "node fileB.js"

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.