How to get the current filename in Node.js

4 Answers

0 votes
const path = require('path');

const fn = path.basename(__filename);


console.log(fn);
     
   
/*
run:
   
app.js
   
*/

 



answered Mar 1, 2020 by avibootz
0 votes
const path = require('path');

console.log(module.filename.slice(__filename.lastIndexOf(path.sep) + 1));
  

   
/*
run:
   
app.js
   
*/

 



answered Mar 1, 2020 by avibootz
0 votes
console.log(__filename.slice(__dirname.length + 1));
     
   
/*
run:
   
app.js
   
*/

 



answered Mar 1, 2020 by avibootz
0 votes
const path = require('path');

const pathObject = path.parse(__filename);

console.log(pathObject.base);

     
   
/*
run:
   
app.js
   
*/

 



answered Mar 1, 2020 by avibootz

Related questions

2 answers 298 views
3 answers 344 views
1 answer 216 views
2 answers 125 views
1 answer 79 views
1 answer 98 views
1 answer 89 views
...