从模块函数中访问调用者脚本的__filename

问题描述 投票:1回答:1

我正在使用node.js.

我想从该脚本调用的模块函数中访问脚本的文件名。例如,

exampleModule.js:

module.exports = function(){
  console.log(__filename);
  // more code which may also use the filename
};

example.js

var exampleModule = require("./exampleModule.js")

exampleModule();

这将记录“path / exampleModule.js”。我想知道如何使它记录“path / example.js”,而我宁愿不必传入__filename作为参数。

提前致谢 :)

javascript node.js module require
1个回答
0
投票

找到了:

module.parent.filename

朋友知道Google正确的事情。

© www.soinside.com 2019 - 2024. All rights reserved.