From c1bee2bef8e1237c8d5fc6904e129ecf1653939c Mon Sep 17 00:00:00 2001 From: Chad Nehemiah Date: Mon, 19 Jun 2023 09:35:42 -0500 Subject: [PATCH] docs: add ESM section to avoid configuration issues (#1782) Related https://github.com/libp2p/js-libp2p/issues/1776 --------- Co-authored-by: Alex Potsides --- doc/GETTING_STARTED.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/doc/GETTING_STARTED.md b/doc/GETTING_STARTED.md index 93f2142113..ce72872059 100644 --- a/doc/GETTING_STARTED.md +++ b/doc/GETTING_STARTED.md @@ -5,6 +5,7 @@ Welcome to libp2p! This guide will walk you through setting up a fully functiona - [Getting Started](#getting-started) - [Install](#install) - [Configuring libp2p](#configuring-libp2p) + - [ESM](#esm) - [Basic setup](#basic-setup) - [Transports](#transports) - [Connection Encryption](#connection-encryption) @@ -29,6 +30,27 @@ npm install libp2p If you're new to libp2p, we recommend configuring your node in stages, as this can make troubleshooting configuration issues much easier. In this guide, we'll do just that. If you're more experienced with libp2p, you may wish to jump to the [Configuration readme](./CONFIGURATION.md). +### ESM + +Since `libp2p@0.37.0` modules are now [ESM-only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c). + +ESM is the module system for JavaScript, it allows us to structure our code in separate files without polluting a global namespace. + +Other systems have tried to fill this gap, notably CommonJS, AMD, RequireJS and others, but ESM is [the official standard format](https://tc39.es/ecma262/#sec-modules) to package JavaScript code for reuse. This means that you need ensure your configuration uses the correct module system, if you are using Typescript, set the [`module` field in your tsconfig](https://www.typescriptlang.org/tsconfig#module) to `ES2022 ` or later e.g. + +```json +{ + "compilerOptions": { + "module": "ES2022", + "esModuleInterop": true, + "target": "ES2022", + "moduleResolution": "node" + } +} +``` + +For more info on enablng ES modules in Node, see [this guide](https://nodejs.org/api/esm.html). + ### Basic setup Now that we have libp2p installed, let's configure the minimum needed to get your node running. The only modules libp2p requires are a [**Transport**][transport] and [**Crypto**][crypto] module. However, we recommend that a basic setup should also have a [**Stream Multiplexer**](streamMuxer) configured, which we will explain shortly. Let's start by setting up a Transport.