【发布时间】:2019-07-09 08:29:34
【问题描述】:
我希望能够在 log.js 文件中签入(file1 或 file2)函数 print_info 执行的位置。
我已经尝试过 module.parent.parent.filename,但即使我正在运行 file2,它也会返回到 file1 的路径。
在节点中可以吗?
我有这些文件:
log.js
function print_info(data){
console.log('Function was called in: ', module.parent.parent.filename) //here I'd like to know where function was executed
console.log(data);
};
file1.js
var print_info = require('log.js');
print_info(my_data);
file2.js
var print_info = require('log.js');
print_info(my_data);
【问题讨论】:
-
尝试在 log.js 中添加
delete require.cache[__filename];。检查它What is the use of module.parent in node.js?
标签: javascript node.js