Skip to content

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

@antonio-ivanovski

Description

@antonio-ivanovski

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions