helia: ERR_PACKAGE_PATH_NOT_EXPORTED: No "exports" main defined in helia/package.json

Error

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /<path>/node_modules/helia/package.json
    at new NodeError (node:internal/errors:399:5)
    at exportsNotFound (node:internal/modules/esm/resolve:361:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:641:13)
    at resolveExports (node:internal/modules/cjs/loader:538:36)
    at Function.Module._findPath (node:internal/modules/cjs/loader:607:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1033:27)
    at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/home/sxia/code/dasset/node_modules/@cspotcode/source-map-support/source-map-support.js:811:30)
    at Function.Module._load (node:internal/modules/cjs/loader:893:27)
    at Module.require (node:internal/modules/cjs/loader:1113:19)
    at require (node:internal/modules/cjs/helpers:103:18) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

main.ts

import { strings } from '@helia/strings';
import { createHelia } from 'helia';

(async () =>{
    const helia = await createHelia();
})()

dependencies: “helia”: “^1.3.8”

“@helia/strings”: “^1.0.0”,

When I just simply run the above code with ts-node main.ts

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 18 (9 by maintainers)

Most upvoted comments

TBH I’m not sure if that’s going to add much value since there an example available of how to configure NestJS to work with Helia, so we know the default settings for NestJS are to build CJS, and we know the config changes needed to make it build ESM, but you can’t apply them because it “breaks the entire project”.

The root of the issue here is that ERR_PACKAGE_PATH_NOT_EXPORTED is caused when CJS code uses require to load an ES module.

To prevent his happening you have to import it, or await import if you have to stick on CJS.

Your TypeScript settings are (I think, please confirm) transpiling await import('helia') to await Promise.resolve().then(() => require('helia')) which just isn’t going to work.

This is not a problem specific to Helia, you’ll have this problem with any module written in ESM that use an exports map.

I suggest fixing your project so it can work with ES modules. This might be as simple as adding '.js' to every imported file from the current project since ESM requires file extensions.

If you won’t do this then the only other thing you can do is try to defeat the transpilation of import to require by using eval:

const { createHelia } = await eval('import("helia")')

just doesn’t work on an existing nestJS project using CommonJS.

I’m guessing that you have a NextJS project, you’ve set "module" to "CommonJS".

I’m guessing that you’ve done something like:

const { createHelia } = await import('helia');

and you’ve checked your output folder and seen it being transpiled to:

const { createHelia } = await Promise.resolve().then(() => require('helia'));

The solution here appears to be to change "moduleResolution" to "Node16" - this prevents the dynamic import from being transpiled to require.

If this does not solve your problem then you are going to have to provide a link to your source code or a reproducible example for us to assist further, because all this guess work has a high cost in time spent and a low probability of success.

PS: let me know if you need a clean nestjs app with helia installed and crashing, i can create it in the next days once back home.

Yes please. We can provide much better help with reproducible code. Thanks!

I’m using import, on a nestjs project. And obviously it doesn’t work, still same error described in this thread.