absdir

Find the absolute directory path of a filename or a module object. No more excuses for __dirname.

Usage no npm install needed!

<script type="module">
  import absdir from 'https://cdn.skypack.dev/absdir';
</script>

README



absdir

Find the absolute directory path of a filename or a module object. No more excuses for __dirname.

Usage

from test/howto.js:

var moduleDir = require('absdir')(module);
equal(pathLib.join(moduleDir, 'howto.js'), module.filename);

API

This module exports one function:

absdir(pathOrModule[, prefixPath])

Without prefixPath, returns the absolute path to the parent directory of pathOrModule. An object with a string-type url property (e.g. your ES6 module's import.meta) is fine, too.

With prefixPath, returns a function that will resolve paths relative to pathOrModule's parent directory and the prefixPath (use . if you don't need a prefix).

 

Better than __dirname

  • Always gives an absolute path, so paths based on that won't crumble when your process changes its working directory.
  • Easily avoid those ugly __. (for JSLint#39)

 

License

ISC