multiformats / js-multiformats

Multiformats interface (multihash, multicodec, multibase and CID)
Other
224 stars 52 forks source link

Is it possible to use this package in commonjs environment? #271

Closed DeRain closed 1 year ago

DeRain commented 1 year ago

Hi, I've been trying to use a package in commonjs environment and got the following error (I assume it's because exports doesn't have require section).

 ERROR: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined

Is it a bug or you don't allow to use this package in commonjs envs? Thank you!

rvagg commented 1 year ago

We only export ESM packages now. You could either go back to version 9 and use that, it's perfectly usable, but you'll just have compatibility issues with other packages that use it. Alternatively you could compile it down using tool that takes ESM and converts to CJS for you; that's not an unreasonable approach if you're really stuck in a CJS environment.

DeRain commented 1 year ago

Thanks you for clarification!

wemeetagain commented 1 year ago

Another option should work, though may not be convenient in your codebase, using await import to import the library:

const { CID, hasher, digest, varint, bytes } = await import('multiformats')
DeRain commented 1 year ago

Thanks all for help! I've decided to go with fork and added CommonJS build: https://github.com/unstoppabledomains/js-multiformats