'Export syntax in NodeJs

I am new to Node.JS. I am knocking some utility functions for an existing application. I have added these functions:

/**
 *
 * @returns the current stacktrace
 */
function getStackTrace() {
    var orig = Error.prepareStackTrace;
    Error.prepareStackTrace = function(_, theCallStack) {
        return theCallStack;
    };
    var err = new Error;
    Error.captureStackTrace(err, arguments.callee);
    var stack = err.stack;
    Error.prepareStackTrace = orig;
    return stack;
}

/**
 * Gets the name of the function which called the function which calls this function.
 * E.g first() calls seconds(), which calls this function; the return value will be the string 'first, since second, 
 * which called this, wants to know who called it.
 * 
 * @param[depth] : Optional, default = 1, allows us to examine the call stack at different depths, should we wish to 
 */
exports.getCallingFunctionName = (depth = 1) => {
    try{
        const theCallStack = getStackTrace();
        if (depth < theCallStack.length)
            return theCallStack[depth].getFunctionName(); 
        else
            return '????';      // ToDo: Should prolly raise an exception
    }

    catch(e)
    {
        //FixMe: CodeMe:
        console.log('Aaaaaaaaaaaaargh!!!! ' + e);
     }
}

and getCallingFunctionName() is called by logCallAndStartProfiling which is declared in a different file, utils.js, as exports.logCallAndStartProfiling = function(){ ... etc.

When I look at the result of getCallingFunctionName(), I see exports.logCallAndStartProfiling whereas I would prefer to see utils.logCallAndStartProfiling. Can I achieve this somehow by redeclaring the export, using a different syntax? Currently, I use var exports = (module.exports = {});.

Btw, for reasons that I won't go into, TypeScript is not currently an option, although I will move to it later, but do not have, and cannot get, permission to do so just yet.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source