Node.js - getting current filename

后端 未结 8 869
执念已碎
执念已碎 2020-12-13 08:04

How to get current file name, function name and line number?

I want to use it for logging/debugging purpose, equivalent to __FILE__, __LINE__

8条回答
  •  心在旅途
    2020-12-13 08:37

    within a module you can do any of the following to get the full path with filename

    this.filename;
    module.filename;
    __filename;
    

    If you just want the actual name with no path or extension you can do something like this.

    module.filename.slice(__filename.lastIndexOf(path.sep)+1, module.filename.length -3);
    

提交回复
热议问题