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 454 views
3 answers 526 views
1 answer 328 views
2 answers 263 views
1 answer 174 views
1 answer 216 views
1 answer 184 views
...