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.