npm.io
1.0.6 • Published 8 years ago

absdir

Licence
ISC
Version
1.0.6
Deps
0
Size
4 kB
Vulns
0
Weekly
0

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

Keywords