title: Error adding @keycloak\keycloak-admin-client package in NestJS application
body: <p>I am new to Typescript and Nest JS, working to integrate Keycloak in my Nest JS application to add users to Keycloak. I have added <strong>@keycloak\keycloak-admin-client 19.x</strong> package to work on Keycloak admin API's. I am getting the below error while executing the Nest JS application :</p>
  <pre><code>    C:\Program Files\nodejs\node.exe .\dist\main.js
      Uncaught NodeError Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: F:\MyProj\node_modules\@keycloak\keycloak-admin-client\lib\index.js
      require() of ES modules is not supported.
      require() of F:\MyProj\node_modules\@keycloak\keycloak-admin-client\lib\index.js from F:\MyProj\dist\user\user.service.js is an ES module file as it is a .js file whose nearest parent package.json contains &quot;type&quot;: &quot;module&quot; which defines all .js files in that package scope as ES modules.
      Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove &quot;type&quot;: &quot;module&quot; from F:\MyProj\node_modules\@keycloak\keycloak-admin-client\package.json.
          at NodeError (&lt;node_internals&gt;/internal/errors.js:279:9)
          at Module._extensions..js (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1102:13)
          at Module.load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:950:32)
          at Module._load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:790:14)
          at Module.require (&lt;node_internals&gt;/internal/modules/cjs/loader.js:974:19)
          at require (&lt;node_internals&gt;/internal/modules/cjs/helpers.js:92:18)
          at &lt;anonymous&gt; (F:\MyProj\src\user\user.service.ts:5:1)
          at Module._compile (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1085:14)
          at Module._extensions..js (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1114:10)
          at Module.load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:950:32)
          at Module._load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:790:14)
          at Module.require (&lt;node_internals&gt;/internal/modules/cjs/loader.js:974:19)
          at require (&lt;node_internals&gt;/internal/modules/cjs/helpers.js:92:18)
          at &lt;anonymous&gt; (F:\MyProj\src\user\user.controller.ts:4:1)
          at Module._compile (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1085:14)
          at Module._extensions..js (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1114:10)
          at Module.load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:950:32)
          at Module._load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:790:14)
          at Module.require (&lt;node_internals&gt;/internal/modules/cjs/loader.js:974:19)
          at require (&lt;node_internals&gt;/internal/modules/cjs/helpers.js:92:18)
          at &lt;anonymous&gt; (F:\MyProj\src\user\user.module.ts:3:1)
          at Module._compile (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1085:14)
          at Module._extensions..js (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1114:10)
          at Module.load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:950:32)
          at Module._load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:790:14)
          at Module.require (&lt;node_internals&gt;/internal/modules/cjs/loader.js:974:19)
          at require (&lt;node_internals&gt;/internal/modules/cjs/helpers.js:92:18)
          at &lt;anonymous&gt; (F:\MyProj\src\app.module.ts:7:1)
          at Module._compile (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1085:14)
          at Module._extensions..js (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1114:10)
          at Module.load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:950:32)
          at Module._load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:790:14)
          at Module.require (&lt;node_internals&gt;/internal/modules/cjs/loader.js:974:19)
          at require (&lt;node_internals&gt;/internal/modules/cjs/helpers.js:92:18)
          at &lt;anonymous&gt; (F:\MyProj\src\main.ts:2:1)
          at Module._compile (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1085:14)
          at Module._extensions..js (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1114:10)
          at Module.load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:950:32)
          at Module._load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:790:14)
          at executeUserEntryPoint (&lt;node_internals&gt;/internal/modules/run_main.js:76:12)
          at &lt;anonymous&gt; (&lt;node_internals&gt;/internal/main/run_main_module.js:17:47)
      Process exited with code 1
  </code></pre>
  <pre><code>This is my package.json
  </code></pre>
  <pre><code>    {
        &quot;name&quot;: &quot;MyProj&quot;,
        &quot;version&quot;: &quot;0.0.1&quot;,
        &quot;description&quot;: &quot;&quot;,
        &quot;author&quot;: &quot;&quot;,
        &quot;private&quot;: true,
        &quot;license&quot;: &quot;UNLICENSED&quot;,
        &quot;scripts&quot;: {
          &quot;prebuild&quot;: &quot;rimraf dist&quot;,
          &quot;build&quot;: &quot;nest build&quot;,
          &quot;format&quot;: &quot;prettier --write \&quot;src/**/*.ts\&quot; \&quot;test/**/*.ts\&quot;&quot;,
          &quot;start&quot;: &quot;nest start&quot;,
          &quot;start:dev&quot;: &quot;nest start --watch&quot;,
          &quot;start:debug&quot;: &quot;nest start --debug --watch&quot;,
          &quot;start:prod&quot;: &quot;node dist/main&quot;,
          &quot;lint&quot;: &quot;eslint \&quot;{src,apps,libs,test}/**/*.ts\&quot; --fix&quot;,
          &quot;test&quot;: &quot;jest&quot;,
          &quot;test:watch&quot;: &quot;jest --watch&quot;,
          &quot;test:cov&quot;: &quot;jest --coverage&quot;,
          &quot;test:debug&quot;: &quot;node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand&quot;,
          &quot;test:e2e&quot;: &quot;jest --config ./test/jest-e2e.json&quot;
        },
        &quot;dependencies&quot;: {
          &quot;@keycloak/keycloak-admin-client&quot;: &quot;^19.0.1&quot;,
          &quot;@nestjs/axios&quot;: &quot;^0.1.0&quot;,
          &quot;@nestjs/common&quot;: &quot;^9.0.0&quot;,
          &quot;@nestjs/config&quot;: &quot;^2.2.0&quot;,
          &quot;@nestjs/core&quot;: &quot;^9.0.11&quot;,
          &quot;@nestjs/platform-express&quot;: &quot;^9.0.0&quot;,
          &quot;reflect-metadata&quot;: &quot;^0.1.13&quot;,
          &quot;rimraf&quot;: &quot;^3.0.2&quot;,
          &quot;rxjs&quot;: &quot;^7.2.0&quot;
        },
        &quot;devDependencies&quot;: {
          &quot;@nestjs/cli&quot;: &quot;^9.0.0&quot;,
          &quot;@nestjs/schematics&quot;: &quot;^9.0.0&quot;,
          &quot;@nestjs/testing&quot;: &quot;^9.0.0&quot;,
          &quot;@types/chai&quot;: &quot;^4.3.3&quot;,
          &quot;@types/express&quot;: &quot;^4.17.13&quot;,
          &quot;@types/jest&quot;: &quot;28.1.4&quot;,
          &quot;@types/mocha&quot;: &quot;^9.1.1&quot;,
          &quot;@types/node&quot;: &quot;^16.0.0&quot;,
          &quot;@types/supertest&quot;: &quot;^2.0.11&quot;,
          &quot;@typescript-eslint/eslint-plugin&quot;: &quot;^5.0.0&quot;,
          &quot;@typescript-eslint/parser&quot;: &quot;^5.0.0&quot;,
          &quot;eslint&quot;: &quot;^8.0.1&quot;,
          &quot;eslint-config-prettier&quot;: &quot;^8.3.0&quot;,
          &quot;eslint-plugin-prettier&quot;: &quot;^4.0.0&quot;,
          &quot;jest&quot;: &quot;28.1.2&quot;,
          &quot;prettier&quot;: &quot;^2.3.2&quot;,
          &quot;source-map-support&quot;: &quot;^0.5.20&quot;,
          &quot;supertest&quot;: &quot;^6.1.3&quot;,
          &quot;ts-jest&quot;: &quot;28.0.5&quot;,
          &quot;ts-loader&quot;: &quot;^9.2.3&quot;,
          &quot;ts-node&quot;: &quot;^10.0.0&quot;,
          &quot;tsconfig-paths&quot;: &quot;4.0.0&quot;,
          &quot;typescript&quot;: &quot;^4.3.5&quot;
        },
        &quot;jest&quot;: {
          &quot;moduleFileExtensions&quot;: [
            &quot;js&quot;,
            &quot;json&quot;,
            &quot;ts&quot;
          ],
          &quot;rootDir&quot;: &quot;src&quot;,
          &quot;testRegex&quot;: &quot;.*\\.spec\\.ts$&quot;,
          &quot;transform&quot;: {
            &quot;^.+\\.(t|j)s$&quot;: &quot;ts-jest&quot;
          },
          &quot;collectCoverageFrom&quot;: [
            &quot;**/*.(t|j)s&quot;
          ],
          &quot;coverageDirectory&quot;: &quot;../coverage&quot;,
          &quot;testEnvironment&quot;: &quot;node&quot;
        }
      }
  </code></pre>
  <p><strong>UPDATE - 1</strong>:</p>
  <ul>
  <li><p>I have added a separate typescript project without Nest JS, installed @keycloak\keycloak-admin-client package and executed the code. Still the same error persists.</p>
  </li>
  <li><p>I have <strong>downgraded to @keycloak\keycloak-admin-client 18.x version</strong> and the error disappeared !!!</p>
  </li>
  </ul>
  <p>Does this mean there is some issue in <strong>@keycloak\keycloak-admin-client 19.x</strong> version? Is there any solutions for working on current version of keycloak-admin-client package?</p>
  <p><strong>UPDATE - 2</strong>:</p>
  <ul>
  <li>I have cloned code of @keycloak\keycloak-admin-client 19.x** version
  and changed &quot;module&quot;: &quot;CommonJS&quot;,&quot;target&quot;: &quot;ES6&quot; in it's
  tsconfig.json file (same as that in my typescript project).</li>
  <li>I have made necessary code changes to fix errors caused due to
  deprecated packages like query-string and methods like
  querystring.stringify methods and few other issues related to axios.
  Finally, I got the following error message :</li>
  </ul>
  <pre><code> Uncaught NodeError Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: d:\keycloak-nodejs-admin-client\node_modules\url-join\lib\url-join.js
  require() of ES modules is not supported.
  require() of e:\NewArchitecture\keycloak-nodejs-admin-client\node_modules\url-join\lib\url-join.js from d:\keycloak-nodejs-admin-client\lib\resources\agent.js is an ES module file as it is a .js file whose nearest parent package.json contains &quot;type&quot;: &quot;module&quot; which defines all .js files in that package scope as ES modules.
  Instead rename url-join.js to end in .cjs, change the requiring code to use import(), or remove &quot;type&quot;: &quot;module&quot; from d:\keycloak-nodejs-admin-client\node_modules\url-join\package.json.
      at NodeError (&lt;node_internals&gt;/internal/errors.js:279:9)
      at Module._extensions..js (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1102:13)
      at Module.load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:950:32)
      at Module._load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:790:14)
      at Module.require (&lt;node_internals&gt;/internal/modules/cjs/loader.js:974:19)
      at require (&lt;node_internals&gt;/internal/modules/cjs/helpers.js:92:18)
      at &lt;anonymous&gt; (d:\keycloak-nodejs-admin-client\src\resources\agent.ts:1:1)
      at Module._compile (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1085:14)
      at Module._extensions..js (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1114:10)
      at Module.load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:950:32)
      at Module._load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:790:14)
      at Module.require (&lt;node_internals&gt;/internal/modules/cjs/loader.js:974:19)
      at require (&lt;node_internals&gt;/internal/modules/cjs/helpers.js:92:18)
      at &lt;anonymous&gt; (d:\keycloak-nodejs-admin-client\src\resources\resource.ts:2:1)
      at Module._compile (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1085:14)
      at Module._extensions..js (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1114:10)
      at Module.load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:950:32)
      at Module._load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:790:14)
      at Module.require (&lt;node_internals&gt;/internal/modules/cjs/loader.js:974:19)
      at require (&lt;node_internals&gt;/internal/modules/cjs/helpers.js:92:18)
      at &lt;anonymous&gt; (d:\keycloak-nodejs-admin-client\src\resources\attackDetection.ts:1:1)
      at Module._compile (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1085:14)
      at Module._extensions..js (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1114:10)
      at Module.load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:950:32)
      at Module._load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:790:14)
      at Module.require (&lt;node_internals&gt;/internal/modules/cjs/loader.js:974:19)
      at require (&lt;node_internals&gt;/internal/modules/cjs/helpers.js:92:18)
      at &lt;anonymous&gt; (d:\keycloak-nodejs-admin-client\src\client.ts:3:1)
      at Module._compile (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1085:14)
      at Module._extensions..js (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1114:10)
      at Module.load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:950:32)
      at Module._load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:790:14)
      at Module.require (&lt;node_internals&gt;/internal/modules/cjs/loader.js:974:19)
      at require (&lt;node_internals&gt;/internal/modules/cjs/helpers.js:92:18)
      at &lt;anonymous&gt; (d:\keycloak-nodejs-admin-client\src\index.ts:2:1)
      at Module._compile (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1085:14)
      at Module._extensions..js (&lt;node_internals&gt;/internal/modules/cjs/loader.js:1114:10)
      at Module.load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:950:32)
      at Module._load (&lt;node_internals&gt;/internal/modules/cjs/loader.js:790:14)
      at executeUserEntryPoint (&lt;node_internals&gt;/internal/modules/run_main.js:76:12)
      at &lt;anonymous&gt; (&lt;node_internals&gt;/internal/main/run_main_module.js:17:47)
  </code></pre>
  <p>It would be helpful if somebody could throw some light on this !!!</p>