JSPM

esm-file-dir

1.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q23413F
  • License MIT

__dirname and __filename in ES Modules without the need for imports

Package Exports

  • esm-file-dir
  • esm-file-dir/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (esm-file-dir) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

esm-file-dir

esm-file-dir is a Node.js package that allows you to easily get __dirname and __filename values in an ES Modules environment, without the need for imports.

Usage

Import package to your root file (e.g., index.js):

// In ".mjs" script or a script under "type": "module" package
import "esm-file-dir";

Retrieve the results:

console.log(__dirname(import.meta));
console.log(__filename(import.meta));

or use __dirname and __filename like under commonjs module:

let __dirname = __dirname(import.meta);
let __filename = __filename(import.meta);

console.log(__dirname, __filename);

Difference from others libraries

There are numerous similar libraries available in the ecosystem, but they typically require importing dirname or filename functions to use. esm-file-dir differentiates itself by directly injecting these functions into the global context, which not only streamlines your code but also enhances usability.

// not doing like this
import { __dirname, __filename } from 'others-libraries';
// instead
import "esm-file-dir";

It's so easy to use!

License

MIT

Github Npm