Home > Back-end >  Export syntax in NodeJs
Export syntax in NodeJs

Time:03-06

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.

CodePudding user response:

I am not sure if you will be able to do this easily.
I prepared a solution, but I am not sure if you will like it :D

const { logCallAndStartProfiling } = require('./utils')
logCallAndStartProfiling()
const utils = require('./other')

exports.logCallAndStartProfiling = function () {
  const functionName = utils.getCallingFunctionName(__filename)
  console.log(functionName)
}
const path = require('path')

function getStackTrace() {
  const orig = Error.prepareStackTrace
  Error.prepareStackTrace = function (_, theCallStack) {
    return theCallStack
  }
  const err = new Error()
  Error.captureStackTrace(err, arguments.callee)
  const stack = err.stack
  Error.prepareStackTrace = orig
  return stack
}

exports.getCallingFunctionName = (fileName, depth = 1) => {
  try {
    const theCallStack = getStackTrace()

    if (depth >= theCallStack.length) return null

    const originalFunctionName = theCallStack[depth].getFunctionName()
    const functionName = originalFunctionName.split('exports.')[1]
    const baseFileName = path.basename(fileName).split('.js')[0]
    return `${baseFileName}.${functionName}`
  } catch (e) {
    return null
  }
}

enter image description here

  • Related