Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
Get the file in which the function was declared in Node.js

I'm relatively new to Node.js. I developed a script in Python, and now I want to convert parts of it to Node.js and as a result some gaps have been created that I will need to understand how to execute in Node.js:

  1. How can I get through the function, the file in which the function was defined?

for example:

// a.js
function foo(){
    console.log("This is function 'foo' which is located in a.js file");
}

// b.js
function bar(){
    console.log("This is function 'bar' which is located in b.js file");
}

// main.js
// The desired function:
getFile(foo); // Will return the path of the a.js file
getFile(bar); // Will return the path of the b.js file
  1. The reason I need to know the location of the file where the function was declared (first question), is to determine whether it is a function that the user wrote or whether it is a built-in function or an external module function.

So my second question is: is there a way to determine the source of the function in the most deterministic way?

  1. Is there a way to get all the existing functions? Including functions that were imported from modules and other files
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can only do that from within the function itself (or a function called by that function). But when you do have access to it, then you can use this trick:

console.log((new Error()).stack);

For example:

function foo1() {
  console.log((new Error()).stack);
}

foo1();

will print:

> node test.js 
Error
    at foo1 (/tmp/test.js:2:16)
    at Object.<anonymous> (/tmp/test.js:5:1)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!