TS & ipfs-core example; v0.15.0 causes error, opposed to v0.14.0

I didn’t make this a GitHub issue right away because I’m still unsure if this is just me or an actual bug.

Using the given example from the js-ipfs examples repo here: js-ipfs-examples/examples/types-use-ipfs-from-ts at master · ipfs-examples/js-ipfs-examples · GitHub

What I noticed was that when going to start a fresh TS project from scratch and using a very vanilla script to start a plain ipfs node I always got the same error. Even if I copied the cloned directory from the example repo into a new directory and build out my project from that skeleton.

What I noticed was that the example’s package.json file defined the ipfs-core module to use version 0.14.0, but when I installed a new version of the ipfs-core module with npm/yarn it adds v0.15.1. Both my vanilla script and the example work just fine after using tsc on the main.ts file and running node on the resulting main.js.

The problem happens consistently with v0.15.1 producing this error:

node:internal/modules/cjs/loader:488
      throw e;
      ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /path/to/project/types-use-ipfs-from-ts/node_modules/ipfs-core/package.json
    at new NodeError (node:internal/errors:372:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:472:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:693:7)
    at resolveExports (node:internal/modules/cjs/loader:482:36)
    at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/path/to/project/types-use-ipfs-from-ts/src/main.js:46:19) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

I can continue my project using version 0.14.0 I think but would like to know if this is just me and whether I should put more time into getting 0.15.0 to work? Or if this is an actual bug that I should open an issue on github about?

Thank you very much!

You want to specify the module loader in your tsconfig using some ESM-compatible option; esnext is a safe bet

"module": "esnext"

I can’t confirm it from the ts docs but I think the default “module” value is commonjs, hence your error.

This seems to be a common problem with latest version of ipfs as they are using esm rather than commonjs. There are workarounds, supposedly, using dynamic imports but I haven’t had any success getting them to work with ipfs and commonjs.

The problem has been asked about multiple times on Github but no definitive solution has been given for those stuck with legacy CommonJS module loading.

ipfs-core 0.15.x shipped as ESM only - please see the upgrade guide.

This shows you are loading ipfs as if it were CJS (e.g. via require):

node:internal/modules/cjs/loader:488

You need to use import instead. Again, please see the upgrade guide.

For TypeScript settings, you need to set at least: "module": "ES2020" and "target": "ES2015" in your tsconfig.json file. You can use "module ": "esnext" as suggested but it’s meaning will change over time.

Awesome! thank you both for the input, I know this is almost 2 weeks later.

This lead to some further reading and larger rabbit hole I probably won’t become 100% familiar with for a while. Reading about the TS config file a bit more, and then that lead into webpack5 vs vite… It just keeps going.

Anyway, got it working now thanks to the above input. One other thing I did was this to get it working in the Nuxt3 frame work and reading the thread it seems needed for react as well: node.js - i tried to Polyfill modules in webpack 5 but not working (Reactjs) - Stack Overflow

Definitely a little in over my head. Although I wasn’t able to get the Vite builder working even after examining the js-ipfs/examples/browser-vite project and implementing the vite.config in my nuxt.config it’s throwing another error…

Nuxt3 is still in RC stage and vite is very new anyway. I’m happy with getting it working in TS, Nuxt3 and Webpack5 for now. May take a second shot at vite at a later date. “Working”, more or less; it compiles, starts the node and I’m able to confirm things like node ID, but my swarm.peers() always returns 4 so I think I’ve not quite implemented it 100%, but that’s probably on me and just gonna take some tweaking.

Thanks for pointing me in the right direction!