如何通过javascript中另一个文件中定义的函数获取当前文件的文件路径?

如何通过javascript中另一个文件中定义的函数获取当前文件的文件路径?,javascript,path,Javascript,Path,假设有两个文件 ./file2.js function scriptpath() return __filename ./file1.js var path = file2.scriptpath() console.log(path) //returns absolute path of file2.js but not file1.js 我希望从调用文件(file1.js)的位置获得文件(file1.js)的绝对路径。我假设您需要file2.j

假设有两个文件

   ./file2.js
    function scriptpath()
    return __filename

   ./file1.js
    var path = file2.scriptpath()
    console.log(path)  //returns absolute path of file2.js but not file1.js

我希望从调用文件(file1.js)的位置获得文件(file1.js)的绝对路径。

我假设您需要file2.js中file1.js的路径。你可以用这个:

./file2.js
function scriptpath(callername)
return callername

./file1.js
var path = file2.scriptpath(__filename)
console.log(path)  //returns path of file1.js