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

Commit 58e6f46

Browse files
authored
docs: add ts section to esm upgrade guide (#4241)
Improve docs around upgrading ts projects to use esm.
1 parent a77e40c commit 58e6f46

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

docs/upgrading/v0.62-v0.63.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
## Table of Contents <!-- omit in toc -->
77

88
- [ESM](#esm)
9+
- [TypeScript and ESM](#typescript-and-esm)
910
- [`[email protected]`](#libp2p037x)
1011
- [PeerIds](#peerids)
1112
- [multiaddrs](#multiaddrs)
@@ -34,6 +35,48 @@ async function loadIpfs () {
3435
}
3536
```
3637

38+
### TypeScript and ESM
39+
40+
When authoring typescript it can often look like you are writing ESM:
41+
42+
```ts
43+
import { create } from 'ipfs-core'
44+
45+
create()
46+
```
47+
48+
When this is transpiled to JavaScript the default settings will emit CJS which will fail at runtime:
49+
50+
```js
51+
"use strict";
52+
exports.__esModule = true;
53+
var ipfs_core_1 = require("ipfs-core");
54+
(0, ipfs_core_1.create)();
55+
```
56+
57+
You may also see errors about private identifiers:
58+
59+
```console
60+
node_modules/@libp2p/interfaces/dist/src/events.d.ts:19:5 - error TS18028: Private identifiers are only available when targeting ECMAScript 2015 and higher.
61+
62+
19 #private;
63+
~~~~~~~~
64+
```
65+
66+
To build correctly with ESM as a target, update your `tsconfig.json` to include the following:
67+
68+
```js
69+
{
70+
"module": "es2020", // ensures output is ESM
71+
"target": "es2020", // support modern features like private identifiers
72+
// other settings
73+
}
74+
```
75+
76+
They must both be set to `es2020` at least, more recent versions will also work.
77+
78+
If in doubt, examine the JavaScript files `tsc` emits and ensure that any `ipfs` modules are being loaded with `import` and not `require`.
79+
3780
3881

3982
`[email protected]` upgrades to `[email protected]`. This is a significant refactor that ports the entire stack to TypeScript and publishes all modules as ESM-only code.

0 commit comments

Comments
 (0)