Sphereon-Opensource / SSI-SDK-crypto-extensions

SSI SDK Crypto Extensions (BBS+, JWK, RSA, EBSI, KMS & DID methods)
Apache License 2.0
2 stars 3 forks source link

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

Open toteto opened 9 months ago

toteto commented 9 months ago

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

nklomp commented 1 month ago

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