calling exports.start in same js file node.js
node.jsnode.js Problem Overview
Hi this is my method in a node js file:
exports.start = function() {
console.log(' in start of sender.js');
});
How can I call this method in the same js file? I tried calling start() and exports.start() but not successful.
node.js Solutions
Solution 1 - node.js
Use this code:
var start = exports.start = function() {
console.log(' in start of sender.js');
});
or
function start() {
console.log(' in start of sender.js');
});
exports.start = start;
//you can call start();
Solution 2 - node.js
exports.start = function(){
console.log('testing...')
}
You can call this method like this
exports.start();
Solution 3 - node.js
You can call the exported function like this
> module.exports.functionName(arguments);
Solution 4 - node.js
Your named function:
var start = function() {
console.log(' in start of sender.js');
});
And later export object:
module.exports = {start : start}
So you can call start()
in the same js file
Solution 5 - node.js
What I'm doing on my computer is the following, and working well - please comment if you think it's a bad idea !
Let's say you're on file.js
const onThisFile = require("./file");
exports.get = async (args) => .... // whatever;
exports.put = async (args) => .... // whatever;
exports.post = async (args) => .... // whatever;
exports.delete = async (args) => .... // whatever;
exports.doSomething = async (args) => onThisFile.get(args)