# Is magic-string ESM-only? Can you require() it?

> magic-string@1.2.3 is ESM-only. ESM-only since 1.0.0 — last CommonJS version is 0.30.21.

Modify strings, generate sourcemaps

Last verified Sep 9, 2026 against magic-string@1.2.3.

magic-string dropped its CommonJS build in 1.0.0 (July 2026). The last version with a plain require()-able CommonJS entry is 0.30.21, published Oct 24, 2025.

On Node.js 20.19+, 22.12+ and newer, require("magic-string") works with the latest version: Node.js loads synchronous ES modules through require(). Verified on Node.js 20, 22, 24 and 26. On Node.js 18, which has no require(esm), require("magic-string") fails with ERR_REQUIRE_ESM.

## What to do

1. On Node.js 20.19+, 22.12+ and newer, require("magic-string") works with the latest version: Node.js loads synchronous ES modules through require(). Verified on Node.js 20, 22, 24 and 26.
2. From a CommonJS file, use a dynamic import inside an async function: const magicString = await import("magic-string"). That works on every Node.js version the package supports.
3. If you need a synchronous require() or run Node.js 18: npm install magic-string@0.30.21, the last CommonJS release (published Oct 24, 2025).
4. TypeScript: types are bundled; install nothing extra.

## Facts

- Latest: 1.2.3 (Aug 26, 2026)
- Module format: ESM-only
- Last CommonJS version: 0.30.21
- ESM-only since: 1.0.0 (Jul 16, 2026)
- TypeScript types: bundled
- Node engines: not declared
- Weekly downloads: 174.5M
- License: MIT

## Runtime test results for magic-string@1.2.3

Installed and executed on Sep 9, 2026 with require("magic-string") from a CommonJS file and import "magic-string" from an ES module.

| Runtime | require("magic-string") | import "magic-string" |
|---|---|---|
| Node.js 18 | fails (ERR_REQUIRE_ESM) | works |
| Node.js 20 | works | works |
| Node.js 22 | works | works |
| Node.js 24 | works | works |
| Node.js 26 | works | works |
| Bun 1.4.0 | works | works |

## How to import magic-string

ES modules:

```js
import * as magicString from "magic-string";
```

Namespace import shown because the ESM entry may have no default export; use named imports for what it exports.

CommonJS:

```js
// Node.js 20.19+, 22.12+ and newer (verified):
const magicString = require("magic-string");

// Any Node.js version the package supports, from an async function:
async function main() {
  const magicString = await import("magic-string");
}

// Or pin the last CommonJS release:
// npm install magic-string@0.30.21
```

## Module format by version

Derived from the type, main and exports fields of every stable release on npm (130 versions; the newest 60 shown).

| Versions | Format | First published |
|---|---|---|
| 1.0.0 – 1.2.3 | ESM-only | Jul 16, 2026 |
| 0.26.0 – 0.30.21 | Dual (CommonJS + ESM) | Mar 3, 2022 |
| 0.22.0 – 0.25.9 | CommonJS | Jul 7, 2017 |

## Frequently asked

**Is magic-string ESM-only?**

magic-string@1.2.3 is ESM-only. magic-string dropped its CommonJS build in 1.0.0 (July 2026). The last version with a plain require()-able CommonJS entry is 0.30.21, published Oct 24, 2025.

**Which version of magic-string still supports CommonJS / require()?**

0.30.21 is the last version of magic-string with a CommonJS build. Install it with "npm install magic-string@0.30.21". Every version from 1.0.0 on is ESM-only.

**How do I use magic-string from a CommonJS file?**

From a CommonJS file, use a dynamic import inside an async function: const magicString = await import("magic-string"). That works on every Node.js version the package supports. On Node.js 20.19+, 22.12+ and newer, require("magic-string") works with the latest version: Node.js loads synchronous ES modules through require(). Verified on Node.js 20, 22, 24 and 26. Alternatively pin magic-string@0.30.21.

**Does magic-string work in Bun?**

Yes. import "magic-string" loaded successfully in Bun 1.4.0 on Sep 9, 2026.

**Does magic-string ship TypeScript types?**

Yes. magic-string bundles its own type declarations; no @types package is needed.

## Links

- [magic-string on npm](https://www.npmjs.com/package/magic-string)
- [Source repository](https://github.com/Rich-Harris/magic-string)
- [HTML version](https://canirequire.com/package/magic-string/)
- [JSON version](https://canirequire.com/package/magic-string.json)

---

For scripts and AI agents: any package is at https://canirequire.com/package/<name>/index.md (Markdown) or https://canirequire.com/package/<name>.json (JSON); the site index is https://canirequire.com/llms.txt. Fetch the URL directly; no search needed.
