# common-es

> provides utilities for nodejs to be able to resolve es modules, such as providing the __dirname and __filename variables

Latest version **1.1.0** (published 2025-07-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install common-es
pnpm add common-es
yarn add common-es
bun add common-es
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2025-07-05 |
| First published | 2023-03-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 1 |
| Unpacked size | 3.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Islam Yamor |
| Maintainers | samislam |
| Keywords | __dirname, __filename, filename, dirname, common-es, es-node, es6, es-next |

## Links

- npm: https://www.npmjs.com/package/common-es
- Repository: https://github.com/samislam/common-es
- Homepage: https://github.com/samislam/common-es#readme
- Issues: https://github.com/samislam/common-es/issues
- npm.io page: https://npm.io/package/common-es

## Dependencies (1)

- [typescript](https://npm.io/package/typescript.md) ^4.9.5

## Recent versions

- 1.1.0 (latest) — 2025-07-05
- 1.0.1 — 2023-04-28
- 1.0.0 — 2023-03-15

## README

# common-es
Provides utilities for Node.js to be able to resolve ES modules, such as providing the `__dirname` and `__filename` variables.
---
When working with Node.js using ES modules, sometimes you'll need the **dirname global variable, or the **filename variable, such as when reading or writing a file.
Unfortunately, this global variable doesn't exist when you have the `"type": "module"` option set in your **package.json** file. In order to get the functionality of these nonexistent variables, you have to do a bit of work manually.
This simple utility provides access to these variables in a **simple** and a **standard** way.
# Usage:
```ts
// # myProjectFile.js
import { getGlobals } from 'common-es'
const { __dirname, __filename } = getGlobals(import.meta.url)
// now you can use __dirname or file name normally as you would do in commonjs
// ...
// ...
```
# API
```
getGlobals(url: string): { __dirname: string, __filename: string }
```
a function that provides the **\_\_dirname** and **\_\_filename** variables.
Expects a string, this string is always the `import.meta.url`.
If you're not familiar with this weird syntax, read this reference on MDN [Here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta)

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