I have something like this:
- MyModule
index.js
myFunction1.js
myFunction2.js
In index.js
file i'm exporting all modules function (myFunction1
and myFunction2
). But, in myFunction2
I use myFunction1
.
If I import the index (all the module) and call it like MyModule.myFunction1
inside myFunction2
, I get an error (function does not exists).
If I import myFunction1.js
and call it like myFunction1()
, I can't make an Stub of it when I'm going to test it.
Any aproach to do something like this?
6 Answers
Answers 1
////////////////////// // File: ./index.js // ////////////////////// const myFunction1 = require('./myFunction1.js'); const myFunction2 = require('./myFunction2.js'); module.exports = { myFunction1, myFunction2 }; //////////////////////////// // File: ./myFunction1.js // //////////////////////////// module.exports = function1(x){ return x; }; //////////////////////////// // File: ./myFunction2.js // //////////////////////////// const myFunction1 = require('./myFunction1.js'); // or dropping the `.js` require('./myFunction1') module.exports = function2(x){ return myFunction1(x); };
Answers 2
index.js
let fnk1 = (a) => { return a + ' 1 ' } let fnk2 = (a) => { return fnk1(a) } module.exports = { fnk1, fnk2 }
test.js
let Mymodule = require('./index') console.log(Mymodule.fnk2('argValue'));
Answers 3
This should do the trick. It would be better if they were in the same file.
module.exports.myFunction1 = function() { ... } module.exports.myFunction2 = function() { module.exports.myFunction1() }
If you need them to be in separate files, you can require function1 inside of the function2 file. The thing is, each file is its own module. From the Node.js docs:
Node.js has a simple module loading system. In Node.js, files and modules are in one-to-one correspondence (each file is treated as a separate module).
Answers 4
Diego's answer works fine, except that OP seems to want to use index.js in myFunction2.js. Use the same code as Diego's for index.js
for index.js (same as Diego's)
const myFunction1 = require('./myFunction1.js'); const myFunction2 = require('./myFunction2.js'); module.exports = { myFunction1, myFunction2 };
for myFunction1.js (small fix)
module.exports = function function1(x){ return x; };
for myFunction2.js (changed to include index.js):
const MyModule = require('./index.js'); module.exports = function function2(x){ return MyModule.myFunction1(x); };
and a test.js :
const myFunction2 = require('./myFunction2.js'); console.log(myFunction2(20))
Answers 5
const myFunction1 = require('./myFunction1.js'); const myFunction2 = require('./myFunction2.js'); module.exports = { myFunction1:myFunction1, myFunction2:function(x){ return myFunction2(x,myFunction1); } } //////////////////////////// // File: ./myFunction1.js // //////////////////////////// module.exports = function1(x){ return x; }; //////////////////////////// // File: ./myFunction2.js // //////////////////////////// module.exports = function2(x,myFunction1){ return myFunction1(x); };
Answers 6
You problem is not clear because you did not post your code. Since, here is a working solution for your problem:
MyModule.js:
function myFunction1() { console.log("- I am myFunction1") } function myFunction2() { console.log("- I am myFunction2") myFunction1() } module.exports = { myFunction1, myFunction2 }
index.js
const MyModule = require('./MyModule') console.log("Call myFunction1") MyModule.myFunction1() console.log("Call myFunction1") MyModule.myFunction2()
If you execute node index.js
, you get the following output:
Call myFunction1 - I am myFunction1 Call myFunction2 - I am myFunction2 - I am myFunction1
0 comments:
Post a Comment