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)
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_EXPORTEDis caused when CJS code usesrequireto load an ES module.To prevent his happening you have to
importit, orawait importif you have to stick on CJS.Your TypeScript settings are (I think, please confirm) transpiling
await import('helia')toawait 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
importtorequireby using eval:I’m guessing that you have a NextJS project, you’ve set
"module"to"CommonJS".I’m guessing that you’ve done something like:
and you’ve checked your output folder and seen it being transpiled to:
The solution here appears to be to change
"moduleResolution"to"Node16"- this prevents the dynamicimportfrom being transpiled torequire.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.
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.