-
Notifications
You must be signed in to change notification settings - Fork 480
Error: No "exports" main defined in <PATH>/libp2p/package.json #1583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Your |
Oops, seems like we needed more information for this issue, please comment with more details or this issue will be closed in 7 days. |
I've created a sample project with a long readme. |
Oops, seems like we needed more information for this issue, please comment with more details or this issue will be closed in 7 days. |
This issue was closed because it is missing author input. |
I've added more details here. One more test case https://github.com/Igx22/p2p-test#option-2-start-compiled-js-from-distsrcindexjs-on-typescript-49 |
I think the fundamental problem here is as you have found, ts-node does not support ESM. Until it does, the only solution you have, if you must use ts-node, is to have your project export CJS but load libp2p (and any other ESM-only module) either from a In your test project, I make the following changes: // tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
// ... other stuff
} And add a cjs file (I called it // src/index.cjs
async function main () {
await import('./p2p-discovery.mjs')
}
main().catch(err => {
console.error(err)
process.exit(1)
}) Then run it, everything is happy: % npx ts-node src/index.cjs
node 1 is listening on:
/ip4/127.0.0.1/tcp/54449/p2p/12D3KooWHaikSqN6i3PVALEVLzCe2uVB7uyRU5n9AAQ29TCF9uWT
/ip4/192.168.1.10/tcp/54449/p2p/12D3KooWHaikSqN6i3PVALEVLzCe2uVB7uyRU5n9AAQ29TCF9uWT
node 2 is listening on:
/ip4/127.0.0.1/tcp/54450/p2p/12D3KooWC85PF1CF68o6HCNEny57bE8kv3rLppPtd4ZRSvuZc9VS
/ip4/192.168.1.10/tcp/54450/p2p/12D3KooWC85PF1CF68o6HCNEny57bE8kv3rLppPtd4ZRSvuZc9VS
Hello p2p world! |
Actually, having said that, if I rename % npx ts-node ./src/test.ts
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/alex/test/p2p-test/src/p2p-discovery.mjs not supported.
Instead change the require of /Users/alex/test/p2p-test/src/p2p-discovery.mjs to a dynamic import() which is available in all CommonJS modules.
at /Users/alex/test/p2p-test/src/index-c.ts:2:40
at async main (/Users/alex/test/p2p-test/src/index-c.ts:2:5) {
code: 'ERR_REQUIRE_ESM'
} The above message means The error message even tells me to use I think you need to wait until |
https://github.com/Igx22/p2p-test/pull/1/files This fix actually helped. |
What are your concerns around this?
This is not recommended, newer features and bug fixes will not be ported to older versions. |
70% of our code base is made up of commonjs / require etc.
Fully agree |
For CJS codebases it's safe to use dynamic imports, it's less safe to stick on old versions. Looks like you solved your problem with the ts-node upgrade & extra config though, so I'm going to close this. |
libp2p version:
0.42.2
Node version:
16.0.0
npm version :
7.10.0
I am trying to use this in typescript to test out. The tsconfig.json I am using:
But it gives me the following error while doing :
import { createLibp2p } from 'libp2p'
Error: No "exports" main defined in /Users/ashispradhan/Desktop/Projects/EPNS/push-storage-node/node_modules/libp2p/package.json
The text was updated successfully, but these errors were encountered: