Skip to main content
22 events
when toggle format what by license comment
Mar 30 at 8:27 history edited jonrsharpe CC BY-SA 4.0
added 35 characters in body
Aug 16, 2024 at 10:31 history edited Kostas Minaidis CC BY-SA 4.0
added 301 characters in body
Oct 27, 2022 at 13:50 comment added Ryan As linked in a comment above, if you get __filename is not defined in ES module scope, try stackoverflow.com/a/50053801/470749
Apr 25, 2022 at 8:14 comment added Remirror My advice: only use path module, never process paths manually by splitting them or similar.
Apr 25, 2019 at 0:38 comment added Dan Dascalescu This no longer works with ES modules.
S Oct 24, 2018 at 15:40 history suggested doom CC BY-SA 4.0
add that filename is absolute resolved path. add examples.
Oct 24, 2018 at 12:09 review Suggested edits
S Oct 24, 2018 at 15:40
Aug 14, 2018 at 18:42 history edited JCOC611 CC BY-SA 4.0
Added more information about each global, improved links to point to better docs
Aug 26, 2017 at 11:47 comment added wayofthefuture If you're using webpack: path.dirname(process.mainModule.filename)
Apr 20, 2017 at 19:40 history edited Kyle Slattery CC BY-SA 3.0
added 1 character in body
Dec 17, 2016 at 12:04 history edited Peter Mortensen CC BY-SA 3.0
Copy edited.
Jun 2, 2016 at 2:48 comment added Terra Ashley FYI: Neither of these will work within the interactive node console, but do within scripts.
Oct 5, 2015 at 9:03 comment added Vyacheslav Cotruta Or require('path').basename(__dirname);
Jun 11, 2015 at 10:53 comment added Burgi Or simply __dirname.split(path.sep).pop()
May 7, 2015 at 19:42 comment added Laoujin Found it: var parts = pathString.split(path.sep); return parts[parts.length - 1] (github.com/joyent/node/issues/1224)
May 7, 2015 at 19:33 comment added Laoujin For those trying @apx solution (like I did:), this solution does not work on Windows.
Mar 30, 2014 at 20:13 comment added 19h @AnthonyMartin __dirname.split("/").pop()
Nov 1, 2013 at 15:18 history edited Kyle Slattery CC BY-SA 3.0
Added links to documentation
Oct 30, 2013 at 20:34 comment added Anthony Martin If you want only the directory name and not the full path, you might do something like this: function getCurrentDirectoryName() { var fullPath = __dirname; var path = fullPath.split('/'); var cwd = path[path.length-1]; return cwd; }
May 15, 2012 at 10:26 history edited Tower CC BY-SA 3.0
added 2 characters in body
Jun 30, 2010 at 17:33 vote accept Kyle Slattery
Jun 28, 2010 at 14:39 history answered Kyle Slattery CC BY-SA 2.5