calling exports.start in same js file node.js

node.js

node.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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpankajView Question on Stackoverflow
Solution 1 - node.jsmicnicView Answer on Stackoverflow
Solution 2 - node.jsAshutosh JhaView Answer on Stackoverflow
Solution 3 - node.jsVIKAS KOHLIView Answer on Stackoverflow
Solution 4 - node.jsnorthernwindView Answer on Stackoverflow
Solution 5 - node.jsarnaudambroView Answer on Stackoverflow