# dirname-filename-esm

> Easy __dirname and __filename alternatives for ES Modules

Latest version **1.1.2** (published 2024-07-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install dirname-filename-esm
pnpm add dirname-filename-esm
yarn add dirname-filename-esm
bun add dirname-filename-esm
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2024-07-11 |
| First published | 2019-11-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 68 |
| Author | rhysd |
| Maintainers | rhysd |
| Keywords | node, esm, __dirname, __filename |

## Links

- npm: https://www.npmjs.com/package/dirname-filename-esm
- Repository: https://github.com/rhysd/dirname-filename-esm
- Homepage: https://github.com/rhysd/dirname-filename-esm#readme
- Issues: https://github.com/rhysd/dirname-filename-esm/issues
- npm.io page: https://npm.io/package/dirname-filename-esm

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 1.1.2 (latest) — 2024-07-11
- 1.1.1 — 2021-05-23
- 1.1.0 — 2021-05-13
- 1.0.2 — 2020-10-12
- 1.0.1 — 2019-12-31
- 1.0.0 — 2019-11-22

## README

`__dirname` and `__filename` for ES Modules on Node.js
======================================================
[![CI][ci-badge]][ci]
[![npm][npm-ver]][npm]

Node.js v13.2.0 was released with support for ES Modules. [The official announcement][announce] explains
the overview.

In ES Modules environment, global variables such as `require`, `exports`, `module.exports`, `__filename`,
`__dirname` are not available. `require`, `exports`, `module.exports` are no longer necessary in favor
of ESM, but we still need `__filename` and `__dirname` which can be calculated from `import.meta.url`.

To make the migration from commonjs to ES Modules smooth, [dirname-filename-esm][npm] provides functions
to create `__dirname` and `__filename` variables easily in an ES Modules environment.

## Installation

```sh
npm install --save dirname-filename-esm
```

## Usage

Usage is very straightforward.

```javascript
// In ".mjs" script or a script under "type": "module" package
import { dirname, filename } from 'dirname-filename-esm';

const __dirname = dirname(import.meta);
const __filename = filename(import.meta);

// Use __dirname and __filename like in a commonjs module
```

Two functions are exported.

- `dirname()` takes `import.meta` object and returns `__dirname` value of the script.
- `filename()` takes `import.meta` object and returns `__filename` value of the script.

These functions return an empty string if `import.meta` doesn't have `url` property.

This package has zero dependency.

This package offers [TypeScript](https://www.typescriptlang.org/) support. Since this package is for
ES Modules environment, ensure that `module` option is set to one of the following values in your `tsconfig.json`.

- `es2020` or later
- `node16` or later

## License

[the MIT License](./LICENSE.txt)


[ci]: https://github.com/rhysd/dirname-filename-esm/actions/workflows/ci.yml
[ci-badge]: https://github.com/rhysd/dirname-filename-esm/actions/workflows/ci.yml/badge.svg
[npm]: https://www.npmjs.com/package/dirname-filename-esm
[npm-ver]: https://badge.fury.io/js/dirname-filename-esm.svg
[announce]: https://medium.com/@nodejs/announcing-core-node-js-support-for-ecmascript-modules-c5d6dc29b663

---
_Source: https://npm.io/package/dirname-filename-esm · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
