Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda