libp2p Error: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No “exports” main defined in …
Use dynamic import instead.
utils.ts
// To deal with ESM modules, we need to use dynamic imports
// Error: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in ...
const dynamicImport = async (packageName: string) => new Function(`return import('${packageName}')`)();
export async function initDynamicImports(libs: string[]) {
return await Promise.all(libs.map((lib) => dynamicImport(lib)));
}
Examples:
- Import libp2p
import defaultsDeep from '@nodeutils/defaults-deep';
import { initDynamicImports } from '../libs/utils';
// import { createLibp2p as create } from 'libp2p';
// import { tcp } from '@libp2p/tcp';
// import { webSockets } from '@libp2p/websockets';
// import { noise } from '@chainsafe/libp2p-noise';
// import { tls } from '@libp2p/tls';
// import { yamux } from '@chainsafe/libp2p-yamux';
// import { gossipsub } from '@chainsafe/libp2p-gossipsub';
// import { bootstrap } from '@libp2p/bootstrap';
// import { mdns } from '@libp2p/mdns';
// import { SignaturePolicy } from '@libp2p/interface';
// import { identify } from '@libp2p/identify';
// https://docs.libp2p.io/guides/getting-started/javascript/
// https://github.com/libp2p/js-libp2p/blob/main/doc/CONFIGURATION.md
// https://github.com/libp2p/js-libp2p-examples/tree/main
export async function createLibp2p(_options) {
const [
libp2p,
{ tcp },
{ webSockets },
{ noise },
{…