Node.js - getting current filename

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

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

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);

Node.js provides a standard API to do so: Path.

Getting the name of the current script is then easy:

var path = require('path');
var scriptName = path.basename(__filename);

console.log(__dirname)
console.log(arguments.callee.toString())

You might also look at console-plus. This adds filename and linenumber to any logging text and has different colours for .log, .info and .error.

To get the file name only. No additional module simply:

// abc.js
console.log(__filename.slice(__dirname.length + 1));

 // abc
console.log(__filename.slice(__dirname.length + 1, -3));