Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda