提问者:小点点

在NestJS应用程序中添加@keyCloak\keyCloak-admin-client包时出错


我是Typescript和NestJS的新手,正在努力将KeyCloak集成到我的NestJS应用程序中,以将用户添加到KeyCloak。我已经添加了@keyCloak\keyCloak-admin-client 19. x包来处理KeyCloak管理员API。我在执行NestJS应用程序时收到以下错误:

    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 "type": "module" 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 "type": "module" from F:\MyProj\node_modules\@keycloak\keycloak-admin-client\package.json.
        at NodeError (<node_internals>/internal/errors.js:279:9)
        at Module._extensions..js (<node_internals>/internal/modules/cjs/loader.js:1102:13)
        at Module.load (<node_internals>/internal/modules/cjs/loader.js:950:32)
        at Module._load (<node_internals>/internal/modules/cjs/loader.js:790:14)
        at Module.require (<node_internals>/internal/modules/cjs/loader.js:974:19)
        at require (<node_internals>/internal/modules/cjs/helpers.js:92:18)
        at <anonymous> (F:\MyProj\src\user\user.service.ts:5:1)
        at Module._compile (<node_internals>/internal/modules/cjs/loader.js:1085:14)
        at Module._extensions..js (<node_internals>/internal/modules/cjs/loader.js:1114:10)
        at Module.load (<node_internals>/internal/modules/cjs/loader.js:950:32)
        at Module._load (<node_internals>/internal/modules/cjs/loader.js:790:14)
        at Module.require (<node_internals>/internal/modules/cjs/loader.js:974:19)
        at require (<node_internals>/internal/modules/cjs/helpers.js:92:18)
        at <anonymous> (F:\MyProj\src\user\user.controller.ts:4:1)
        at Module._compile (<node_internals>/internal/modules/cjs/loader.js:1085:14)
        at Module._extensions..js (<node_internals>/internal/modules/cjs/loader.js:1114:10)
        at Module.load (<node_internals>/internal/modules/cjs/loader.js:950:32)
        at Module._load (<node_internals>/internal/modules/cjs/loader.js:790:14)
        at Module.require (<node_internals>/internal/modules/cjs/loader.js:974:19)
        at require (<node_internals>/internal/modules/cjs/helpers.js:92:18)
        at <anonymous> (F:\MyProj\src\user\user.module.ts:3:1)
        at Module._compile (<node_internals>/internal/modules/cjs/loader.js:1085:14)
        at Module._extensions..js (<node_internals>/internal/modules/cjs/loader.js:1114:10)
        at Module.load (<node_internals>/internal/modules/cjs/loader.js:950:32)
        at Module._load (<node_internals>/internal/modules/cjs/loader.js:790:14)
        at Module.require (<node_internals>/internal/modules/cjs/loader.js:974:19)
        at require (<node_internals>/internal/modules/cjs/helpers.js:92:18)
        at <anonymous> (F:\MyProj\src\app.module.ts:7:1)
        at Module._compile (<node_internals>/internal/modules/cjs/loader.js:1085:14)
        at Module._extensions..js (<node_internals>/internal/modules/cjs/loader.js:1114:10)
        at Module.load (<node_internals>/internal/modules/cjs/loader.js:950:32)
        at Module._load (<node_internals>/internal/modules/cjs/loader.js:790:14)
        at Module.require (<node_internals>/internal/modules/cjs/loader.js:974:19)
        at require (<node_internals>/internal/modules/cjs/helpers.js:92:18)
        at <anonymous> (F:\MyProj\src\main.ts:2:1)
        at Module._compile (<node_internals>/internal/modules/cjs/loader.js:1085:14)
        at Module._extensions..js (<node_internals>/internal/modules/cjs/loader.js:1114:10)
        at Module.load (<node_internals>/internal/modules/cjs/loader.js:950:32)
        at Module._load (<node_internals>/internal/modules/cjs/loader.js:790:14)
        at executeUserEntryPoint (<node_internals>/internal/modules/run_main.js:76:12)
        at <anonymous> (<node_internals>/internal/main/run_main_module.js:17:47)
    Process exited with code 1
This is my package.json
    {
      "name": "MyProj",
      "version": "0.0.1",
      "description": "",
      "author": "",
      "private": true,
      "license": "UNLICENSED",
      "scripts": {
        "prebuild": "rimraf dist",
        "build": "nest build",
        "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
        "start": "nest start",
        "start:dev": "nest start --watch",
        "start:debug": "nest start --debug --watch",
        "start:prod": "node dist/main",
        "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
        "test": "jest",
        "test:watch": "jest --watch",
        "test:cov": "jest --coverage",
        "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
        "test:e2e": "jest --config ./test/jest-e2e.json"
      },
      "dependencies": {
        "@keycloak/keycloak-admin-client": "^19.0.1",
        "@nestjs/axios": "^0.1.0",
        "@nestjs/common": "^9.0.0",
        "@nestjs/config": "^2.2.0",
        "@nestjs/core": "^9.0.11",
        "@nestjs/platform-express": "^9.0.0",
        "reflect-metadata": "^0.1.13",
        "rimraf": "^3.0.2",
        "rxjs": "^7.2.0"
      },
      "devDependencies": {
        "@nestjs/cli": "^9.0.0",
        "@nestjs/schematics": "^9.0.0",
        "@nestjs/testing": "^9.0.0",
        "@types/chai": "^4.3.3",
        "@types/express": "^4.17.13",
        "@types/jest": "28.1.4",
        "@types/mocha": "^9.1.1",
        "@types/node": "^16.0.0",
        "@types/supertest": "^2.0.11",
        "@typescript-eslint/eslint-plugin": "^5.0.0",
        "@typescript-eslint/parser": "^5.0.0",
        "eslint": "^8.0.1",
        "eslint-config-prettier": "^8.3.0",
        "eslint-plugin-prettier": "^4.0.0",
        "jest": "28.1.2",
        "prettier": "^2.3.2",
        "source-map-support": "^0.5.20",
        "supertest": "^6.1.3",
        "ts-jest": "28.0.5",
        "ts-loader": "^9.2.3",
        "ts-node": "^10.0.0",
        "tsconfig-paths": "4.0.0",
        "typescript": "^4.3.5"
      },
      "jest": {
        "moduleFileExtensions": [
          "js",
          "json",
          "ts"
        ],
        "rootDir": "src",
        "testRegex": ".*\\.spec\\.ts$",
        "transform": {
          "^.+\\.(t|j)s$": "ts-jest"
        },
        "collectCoverageFrom": [
          "**/*.(t|j)s"
        ],
        "coverageDirectory": "../coverage",
        "testEnvironment": "node"
      }
    }

更新-1:

>

  • 我添加了一个没有NestJS的单独typescript项目,安装了@keyCloak\keyCloak-admin-client包并执行了代码。仍然存在相同的错误。

    我已经降级到@keyCloak\keyCloak-admin-client 18. x版本,错误消失了!!!

    这是否意味着@keyCloak\keycroak-admin-client 19. x版本存在一些问题?是否有任何解决方案可以处理当前版本的keycroak-admin-client包?

    更新-2:

    • 我克隆了@keyCloak\keyCloak-admin-client 19. x**版本的代码,并在tsconfig.json文件中更改了“module”:“Common JS”、“target”:“ES6”(与我的typescript项目中的相同)。
    • 我已经进行了必要的代码更改来修复由于querystring等已弃用的包和querystring. stringify方法等方法以及与axios相关的其他一些问题而导致的错误。最后,我收到了以下错误消息:
     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 "type": "module" 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 "type": "module" from d:\keycloak-nodejs-admin-client\node_modules\url-join\package.json.
        at NodeError (<node_internals>/internal/errors.js:279:9)
        at Module._extensions..js (<node_internals>/internal/modules/cjs/loader.js:1102:13)
        at Module.load (<node_internals>/internal/modules/cjs/loader.js:950:32)
        at Module._load (<node_internals>/internal/modules/cjs/loader.js:790:14)
        at Module.require (<node_internals>/internal/modules/cjs/loader.js:974:19)
        at require (<node_internals>/internal/modules/cjs/helpers.js:92:18)
        at <anonymous> (d:\keycloak-nodejs-admin-client\src\resources\agent.ts:1:1)
        at Module._compile (<node_internals>/internal/modules/cjs/loader.js:1085:14)
        at Module._extensions..js (<node_internals>/internal/modules/cjs/loader.js:1114:10)
        at Module.load (<node_internals>/internal/modules/cjs/loader.js:950:32)
        at Module._load (<node_internals>/internal/modules/cjs/loader.js:790:14)
        at Module.require (<node_internals>/internal/modules/cjs/loader.js:974:19)
        at require (<node_internals>/internal/modules/cjs/helpers.js:92:18)
        at <anonymous> (d:\keycloak-nodejs-admin-client\src\resources\resource.ts:2:1)
        at Module._compile (<node_internals>/internal/modules/cjs/loader.js:1085:14)
        at Module._extensions..js (<node_internals>/internal/modules/cjs/loader.js:1114:10)
        at Module.load (<node_internals>/internal/modules/cjs/loader.js:950:32)
        at Module._load (<node_internals>/internal/modules/cjs/loader.js:790:14)
        at Module.require (<node_internals>/internal/modules/cjs/loader.js:974:19)
        at require (<node_internals>/internal/modules/cjs/helpers.js:92:18)
        at <anonymous> (d:\keycloak-nodejs-admin-client\src\resources\attackDetection.ts:1:1)
        at Module._compile (<node_internals>/internal/modules/cjs/loader.js:1085:14)
        at Module._extensions..js (<node_internals>/internal/modules/cjs/loader.js:1114:10)
        at Module.load (<node_internals>/internal/modules/cjs/loader.js:950:32)
        at Module._load (<node_internals>/internal/modules/cjs/loader.js:790:14)
        at Module.require (<node_internals>/internal/modules/cjs/loader.js:974:19)
        at require (<node_internals>/internal/modules/cjs/helpers.js:92:18)
        at <anonymous> (d:\keycloak-nodejs-admin-client\src\client.ts:3:1)
        at Module._compile (<node_internals>/internal/modules/cjs/loader.js:1085:14)
        at Module._extensions..js (<node_internals>/internal/modules/cjs/loader.js:1114:10)
        at Module.load (<node_internals>/internal/modules/cjs/loader.js:950:32)
        at Module._load (<node_internals>/internal/modules/cjs/loader.js:790:14)
        at Module.require (<node_internals>/internal/modules/cjs/loader.js:974:19)
        at require (<node_internals>/internal/modules/cjs/helpers.js:92:18)
        at <anonymous> (d:\keycloak-nodejs-admin-client\src\index.ts:2:1)
        at Module._compile (<node_internals>/internal/modules/cjs/loader.js:1085:14)
        at Module._extensions..js (<node_internals>/internal/modules/cjs/loader.js:1114:10)
        at Module.load (<node_internals>/internal/modules/cjs/loader.js:950:32)
        at Module._load (<node_internals>/internal/modules/cjs/loader.js:790:14)
        at executeUserEntryPoint (<node_internals>/internal/modules/run_main.js:76:12)
        at <anonymous> (<node_internals>/internal/main/run_main_module.js:17:47)
    

    如果有人能对此有所了解,那将是有帮助的!!!


  • 共2个答案

    匿名用户

    当我从18. x切换到19.x时,我遇到了类似的问题。我观察了你的帖子,希望有人能为我解决这个问题:)但最后我自己做了研究。

    开发这个库的人在升级版本18时将模块系统从通用JS更改为ES模块-

    所以在我的例子中,我“升级”了我的项目,从Common JS模块到ES模块,最后它工作了。但是…更改模块系统并不像只更改TSconfig那么容易(将“模块”从“Common JS”更改为“ESNext”是第一步)。你可以面对更多的问题。我的项目很小,所以我在有限的时间内完成了。我不知道这是否适合你。如何克服从Common JS切换到ES模块时的问题,你可以阅读例如这里:

    • 有没有办法在TS编译文件中结合ES6导入和通用JS要求?
    • 或者这里:https://electerious.medium.com/from-commonjs-to-es-modules-how-to-modernize-your-node-js-app-ad8cdd4fb662

    关于你的UPDATE-2还有一件事。

    您将@keyCloak\keycroak-admin-client库设置为Common JS模块的想法很好-然后您可以从项目中导入Common JS模块。但正如我从您附加的代码中了解到的那样:

         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 "type": "module" 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 "type": "module" from d:\keycloak-nodejs-admin-client\node_modules\url-join\package.json.
    

    一些@keyCloak\keyCloak-admin-client依赖(url-join)使用ES模块,因此@keyCloak\keyCloak-admin-client也需要使用ES模块系统。因此,如果您无法在项目中切换模块系统,并希望将模块系统切换回@keyCloak\keyCloak-admin-client中的Common JS,您还需要注意它的依赖…

    ES模块越受欢迎,使用它的npm库就越多,它就会迫使我们开发人员将项目中的模块系统切换到ES模块:)

    匿名用户

    不幸的是,NestJS仍然不支持导入ES模块,因此我们被迫继续使用以前的版本(18.0.2)