Get current javascript file name and line number

Is there a standard way to access the current script file name?

Is there some kind __FILE__, and __LINE__in C ++ , or the PHP .

If there is no standard way to do this, what tools allow you to add such functions to .js files (preprocessing)?

I am not looking for browser specific solutions (i.e. ) ReferenceError: document is not defined

+4
source share
3 answers

I'm not sure what you are using, but in node.js you can do it like this

file.js

var path = require("path");
console.log(path.basename(__filename));
// => file.js

There is no way to do this in the browser though.

+4

NodeJS, __filename module.filename, .

+2

Yes there is __FILE__in php. You can use basename ( __FILE__, ".php") to have a file name and disable the extension if you want.

-2
source

All Articles