Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

docs: add ts section to esm upgrade guide #4241

Merged
merged 1 commit into from
Oct 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions docs/upgrading/v0.62-v0.63.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
## Table of Contents <!-- omit in toc -->

- [ESM](#esm)
- [TypeScript and ESM](#typescript-and-esm)
- [`libp2p@0.37.x`](#libp2p037x)
- [PeerIds](#peerids)
- [multiaddrs](#multiaddrs)
Expand Down Expand Up @@ -34,6 +35,48 @@ async function loadIpfs () {
}
```

### TypeScript and ESM

When authoring typescript it can often look like you are writing ESM:

```ts
import { create } from 'ipfs-core'

create()
```

When this is transpiled to JavaScript the default settings will emit CJS which will fail at runtime:

```js
"use strict";
exports.__esModule = true;
var ipfs_core_1 = require("ipfs-core");
(0, ipfs_core_1.create)();
```

You may also see errors about private identifiers:

```console
node_modules/@libp2p/interfaces/dist/src/events.d.ts:19:5 - error TS18028: Private identifiers are only available when targeting ECMAScript 2015 and higher.

19 #private;
~~~~~~~~
```

To build correctly with ESM as a target, update your `tsconfig.json` to include the following:

```js
{
"module": "es2020", // ensures output is ESM
"target": "es2020", // support modern features like private identifiers
// other settings
}
```

They must both be set to `es2020` at least, more recent versions will also work.

If in doubt, examine the JavaScript files `tsc` emits and ensure that any `ipfs` modules are being loaded with `import` and not `require`.

## `libp2p@0.37.x`

`ipfs@0.63.x` upgrades to `libp2p@0.37.x`. This is a significant refactor that ports the entire stack to TypeScript and publishes all modules as ESM-only code.
Expand Down