# esm-utils

> Utilities you'll need when migrating to ESModule.

Latest version **4.4.2** (published 2025-04-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install esm-utils
pnpm add esm-utils
yarn add esm-utils
bun add esm-utils
```

## Health

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

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

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.4.2 |
| Published | 2025-04-29 |
| First published | 2021-04-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 2 |
| Unpacked size | 15.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 25 |
| Author | fisker Cheung |
| Maintainers | fisker |

## Links

- npm: https://www.npmjs.com/package/esm-utils
- Repository: https://github.com/fisker/esm-utils
- Homepage: https://github.com/fisker/esm-utils#readme
- Issues: https://github.com/fisker/esm-utils/issues
- Funding: https://github.com/fisker/esm-utils?sponsor=1
- npm.io page: https://npm.io/package/esm-utils

## Dependencies (2)

- [url-or-path](https://npm.io/package/url-or-path.md) ^2.6.1
- [import-meta-resolve](https://npm.io/package/import-meta-resolve.md) ^4.1.0

## Recent versions

- 4.4.2 (latest) — 2025-04-29
- 4.4.1 — 2025-04-28
- 4.4.0 — 2025-04-28
- 4.3.0 — 2024-05-13
- 4.2.1 — 2023-11-06
- 4.2.0 — 2023-11-06
- 4.1.2 — 2023-03-13
- 4.1.1 — 2022-11-24
- 4.1.0 — 2022-08-17
- 4.0.0 — 2022-05-07
- 3.2.0 — 2022-05-07
- 3.1.1 — 2022-04-29
- 3.1.0 — 2022-04-27
- 3.0.0 — 2022-02-11
- 2.2.0 — 2022-02-10
- … 11 more at https://npm.io/package/esm-utils/versions

## README

# esm-utils

[![Build Status][github_actions_badge]][github_actions_link]
[![Coverage][coveralls_badge]][coveralls_link]
[![Npm Version][package_version_badge]][package_link]
[![MIT License][license_badge]][license_link]

[github_actions_badge]: https://img.shields.io/github/actions/workflow/status/fisker/esm-utils/continuous-integration.yml?branch=main&style=flat-square
[github_actions_link]: https://github.com/fisker/esm-utils/actions?query=branch%3Amain
[coveralls_badge]: https://img.shields.io/coveralls/github/fisker/esm-utils/main?style=flat-square
[coveralls_link]: https://coveralls.io/github/fisker/esm-utils?branch=main
[license_badge]: https://img.shields.io/npm/l/esm-utils.svg?style=flat-square
[license_link]: https://github.com/fisker/esm-utils/blob/main/license
[package_version_badge]: https://img.shields.io/npm/v/esm-utils.svg?style=flat-square
[package_link]: https://www.npmjs.com/package/esm-utils

> Utilities you'll need when migrating to ESModule.

## Install

```bash
yarn add esm-utils
```

## Usage

<!-- prettier-ignore -->
```js
import createEsmUtils from 'esm-utils'

const {
  dirname,
  filename,
  require,
  importModule,
  resolve,
  readJson,
  readJsonSync,
} = createEsmUtils(import.meta)
```

```js
/* Those named exports only accept absolute path or URL */
import {
  importModule,
  readJson,
  loadJson,
  readJsonSync,
  loadJsonSync,
} from 'esm-utils'
```

## API

### `createEsmUtils(import.meta | URL | 'string')`

Returns an `object` with the following properties:

- `dirname` (alias `__dirname`)
- `filename` (alias `__filename`)
- `require`
- `importModule` (alias `import`)
- `resolve`
- `readJson` (alias `loadJson`)
- `readJsonSync` (alias `loadJsonSync`)

**Please read [this note](#you-dont-need-dirname-and-filename) before you use `dirname` and `filename`**

Sync version of `readJson`.

### `utils.importModule(string | URL, options?)`

Same as `import()`, but accepts absolute path (on Windows, `import('C:\\foo.js')` error throws when pass a absolute path starts with a drive letter).

#### `options.traceSyntaxError`

type: `boolean`\
default: `false`

Due to [this Node.js issue](https://github.com/nodejs/modules/issues/471), Node.js does not emit the location of the syntax error in the error thrown in dynamic `import()`.

When set `traceSyntaxError: true`, we'll try to get a better error message by running `node <file>` in a child process.

### `utils.readJson(string | URL)`

Returns `Promise<jsonObject>`.

### `utils.readJsonSync(string | URL)`

Sync version of `utils.readJson`

### `utils.resolve(string | URL)`

Ponyfill for [`import.meta.resolve`](https://nodejs.org/api/esm.html#esm_import_meta_resolve_specifier_parent).

If `import.meta.resolve` exits, use it directly, otherwise returns a simple wrapper of [import-meta-resolve](https://github.com/wooorm/import-meta-resolve).

## Import json file

With [`Import Assertions`](https://github.com/tc39/proposal-import-assertions)

```js
import foo from './foo.json' assert {type: 'json'}
```

```json
await import('./foo.json', {assert: {type: 'json'}})
```

With `require`, like CommonJS

```js
import createEsmUtils from 'esm-utils'

const {require} = createEsmUtils(import.meta)
const foo = require('./foo.json')
```

With `readJson` or `readJsonSync`

```js
import createEsmUtils from 'esm-utils'

const {readJson} = createEsmUtils(import.meta)
const foo = await readJson('./foo.json')
```

```js
import createEsmUtils from 'esm-utils'

const {readJsonSync} = createEsmUtils(import.meta)
const foo = readJsonSync('./foo.json')
```

## `importModule()`

Same as `utils.importModule()`, but only accept absolute path or URL.

## `readJson()` (alias `loadJson`)

Same as `utils.readJson()`, but only accept absolute path or URL.

## `readJsonSync()` (alias `loadJsonSync`)

Same as `utils.readJsonSync()`, but only accept absolute path or URL.

## You don't need `dirname` and `filename`

The `dirname` and `filename` supposed to be a quick solution when migrating to ES Modules. In most cases, you don't need them, because many APIs accept `URL` directly.

<!-- prettier-ignore -->
```js
/* BAD */
import fs from 'node:fs/promises'
import path from 'node:path'
import createEsmUtils from 'esm-utils'

const {dirname} = createEsmUtils(import.meta)
const buffer = await fs.readFile(
  path.join(dirname, './path/to/file')
)
```

<!-- prettier-ignore -->
```js
/* GOOD */
import fs from 'node:fs/promises'

const buffer = await fs.readFile(
  new URL('./path/to/file', import.meta.url)
)
```

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