libp2p / js-libp2p

The JavaScript Implementation of libp2p networking stack.
https://libp2p.github.io/js-libp2p/
Other
2.34k stars 447 forks source link

Error [ERR_PACKAGE_PATH_NOT_EXPORTED] in libp2p while using NestJS #2746

Closed fitouch closed 1 month ago

fitouch commented 1 month ago

Severity:

High - The main functionality of the application does not work, API breakage, repo format breakage, etc.

Description:

I am integrating libp2p into a NestJS project, and I encountered the following error when trying to import and use libp2p:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /path/to/project/node_modules/libp2p/package.json
    at exportsNotFound (node:internal/modules/esm/resolve:304:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:594:13)
    at resolveExports (node:internal/modules/cjs/loader:634:36)
    at Module._findPath (node:internal/modules/cjs/loader:724:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1211:27)
    at Module._load (node:internal/modules/cjs/loader:1051:27)
    at Module.require (node:internal/modules/cjs/loader:1311:19)
    at require (node:internal/modules/helpers:179:18)
    at Object.<anonymous> (/path/to/project/src/fitouch/fitouch.service.ts:3:26)
    at Module._compile (node:internal/modules/cjs/loader:1469:14)

Steps to reproduce the error:

  1. Created a NestJS project using the CLI: nest new test-project
  2. Installed libp2p and other related dependencies: npm install libp2p @libp2p/webrtc @libp2p/websockets @chainsafe/libp2p-noise @chainsafe/libp2p-yamux @libp2p/circuit-relay-v2
  3. Added libp2p to a service file:
    
    import { Injectable } from '@nestjs/common';
    import { createLibp2p } from 'libp2p';

@Injectable() export class MyService { async onModuleInit() { const node = await createLibp2p({ // libp2p node configuration }); } }


4. When I start the NestJS application (```npm run start```), I receive the error mentioned above.
tabcat commented 1 month ago

https://stackoverflow.com/questions/74830166/unable-to-import-esm-module-in-nestjs

At first glance this looks to be an issue with NestJS not supporting ESM-only modules. (libp2p only exports esm, not cjs)

fitouch commented 1 month ago

@tabcat Thank you for the clarification! That clears things up. I’ll go ahead and close this issue.