Soy relativamente nuevo en Node.js. Desarrollé un script en Python, y ahora quiero convertir partes de él a Node.js y, como resultado, se han creado algunos vacíos que tendré que entender cómo ejecutar en Node.js:
por ejemplo:
// 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 fileEntonces mi segunda pregunta es: ¿hay alguna manera de determinar la fuente de la función de la manera más determinista?
Solo puede hacer eso desde dentro de la función misma (o una función llamada por esa función). Pero cuando tenga acceso a él, puede usar este truco:
console.log((new Error()).stack);Por ejemplo:
function foo1() { console.log((new Error()).stack); } foo1();imprimirá:
> 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