Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid CJS build, require used for importing ES module #19

Open
antonio-ivanovski opened this issue Nov 21, 2023 · 1 comment
Open

Comments

@antonio-ivanovski
Copy link

I have a project that is declared as ESM module (it has "type":"module") in package.json. When I import and try to use @sphereon/ssi-sdk-ext.did-resolver-key I get the error:

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/projects/stackblitz-starters-6hjrdu/node_modules/nist-weierstrauss/lib/index.js from /home/projects/stackblitz-starters-6hjrdu/node_modules/@sphereon/ssi-sdk-ext.did-resolver-key/dist/drivers/secp256r1.js not supported.
Instead change the require of index.js in /home/projects/stackblitz-starters-6hjrdu/node_modules/@sphereon/ssi-sdk-ext.did-resolver-key/dist/drivers/secp256r1.js to a dynamic import() which is available in all CommonJS modules.

This is because the version of nist-weierstrauss that is used (v1.6.1), has only ESM builds available. This should be OK, but ssi-sdk-ext.did-resolver-key is distributing only CJS and the import it is making for the nist-weierstrauss uses the require statement. Importing ESM modules with require is not allowed.

Minimum reproduction

https://stackblitz.com/edit/stackblitz-starters-6hjrdu

Workaround

As a workaround for my case I have downgraded the nist-weierstrauss to v1.4.1, it is their last delivery as CJS library. For now seems ok.

An alternative workaround, if you have TS setup, is to patch the ssi-sdk-ext.did-resolver-key delivery that will list "main":"src/index.ts" in the package.json. This will take the TS source code directly and transpile it to ESM.

Solution

  • Configure tsc to output ESM instead of CJS (or run it two times to output both)
  • OR, use tsup or esbuild or rollup to create delivery for both CJS and ESM
nklomp added a commit that referenced this issue Aug 1, 2024
…SM only. refs #19 (should have a proper solution soon)
@nklomp
Copy link
Contributor

nklomp commented Aug 1, 2024

Sorry, for not responding earlier. Totally overlooked the issues in this repo and we had no issues in RN because of babel.

Implemented a workaround, as we cannot move to ESM only yet. We will move to both CJS and ESM soon though, so then we can properly fix it. Will keep this ticket open

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants