anonrig / nestjs-keycloak-admin

Keycloak client and admin provider for Nest.js applications with built-in User Managed Access (UMA) and ACL support.
https://npmjs.com/package/nestjs-keycloak-admin
MIT License
181 stars 25 forks source link

ERR_REQUIRE_ESM #161

Closed cybercoder closed 2 years ago

cybercoder commented 2 years ago
Error [ERR_REQUIRE_ESM]: require() of ES Module ./node_modules/@keycloak/keycloak-admin-client/lib/index.js from ./node_modules/nestjs-keycloak-admin/dist/main/service.js not supported.
Instead change the require of index.js in ./node_modules/nestjs-keycloak-admin/dist/main/service.js to a dynamic import() which is available in all CommonJS modules.

platform: mac os node version: v16.17.0 nestjs version: 9.0 nestjs-keycloak-admin version: 1.8.7 keycloak version: 19.0.1 keycloak docker image: quay.io/keycloak/keycloak:19.0.1

anonrig commented 2 years ago

Hey @cybercoder

This is related to @keycloak/keycloak-admin-client making the decision to distribute their code with only ESM support (without CJS).

Please look into the issues https://github.com/keycloak/keycloak-nodejs-admin-client/issues/523, https://github.com/keycloak/keycloak-nodejs-admin-client/issues/519 and https://github.com/keycloak/keycloak-nodejs-admin-client/issues/487

cybercoder commented 2 years ago

for a workaround I've added:

  "resolutions": {
    "@keycloak/keycloak-admin-client": "^18.0.2"
  },

to my package.json file and run yarn, Also added graphql package. And it's working. But it hadn't solve my problem with:

WARN [KeycloakService] Missing token set on refreshGrant.

How ever, thanks for your attention.

It seems the refreshGrunt function is calling somewhere before initialize, So for another workaround I've added

    async refreshGrant() {
        if (!this.tokenSet) await this.initialize();
 ....

to service.js file And comment await client.initialize(); in the module.js file.

anonrig commented 2 years ago

Can you open a pull request if this needs to be resolved on our side?

anonrig commented 2 years ago

I've added a part to README to tell developers about the decision of ESM, and released a new version to stop distributing CJS on 2.0.0 (https://github.com/anonrig/nestjs-keycloak-admin/releases/tag/v2.0.0)