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

Error: Cannot find module 'graphql' #160

Closed MichaHumbel closed 2 years ago

MichaHumbel commented 2 years ago

We get the following error when trying to start our application after installing the nestjs-keycloak-admin package:

Full Error

Error: Cannot find module 'graphql'
- root/node_modules/@nestjs/graphql/dist/decorators/directive.decorator.js
- root/node_modules/@nestjs/graphql/dist/decorators/index.js
- root/node_modules/@nestjs/graphql/dist/index.js
- root/node_modules/nestjs-keycloak-admin/dist/main/utils/extract-request.js
- root/node_modules/nestjs-keycloak-admin/dist/main/guards/auth.guard.js
- root/node_modules/nestjs-keycloak-admin/dist/main/index.js
- root/dist/common/auth/auth.module.js
- root/dist/app.module.js
- root/dist/main.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (root/node_modules/@nestjs/graphql/dist/decorators/directive.decorator.js:4:19)
    at Module._compile (node:internal/modules/cjs/loader:1099:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)

We can't use nestjs-keycloak-admin without installing graphql, even though we don't use graphql. It runs after installing graphql manually as a dependency in our project.

Is there any way to use this package without installing graphql manually?

anonrig commented 2 years ago

Hey @MichaHumbel, which version are you currently using?

MichaHumbel commented 2 years ago

Hey @MichaHumbel, which version are you currently using?

nestjs-keycloak-admin: 1.8.6 Node: v17.8.0

anonrig commented 2 years ago

I don't think it's possible in a not-ugly way. I totally understand where this is coming from. What I can come up with is:

  1. You don't want to increase the node_modules size
  2. You don't want to install any package you don't use

Whole logic for detecting whether or not a request is coming from GraphQL is implemented in utils/extract-request.ts (https://github.com/anonrig/nestjs-keycloak-admin/blob/master/src/utils/extract-request.ts#L5)

Since extractRequest function sits on top of the core logic of Nest.js applications (in all guards), I could not find a solution that does not duplicate existing code.

I believe there are some of the possible solutions (which might not be the best ones):

  1. Split each guard to GraphQL and HTTP: Importing each guard from respective places like nestjs-keycloak-admin/graphql/guards and nestjs-keycloak-admin/http/guards
  2. Dynamically load @nestjs/graphql package if the package is installed. If not, do not even try to understand if the request is a graphql request.

I'd be happy to discuss all possible solutions and reduce the size of this package.

P1K3N commented 2 years ago

same question here.... I'm not using graphQL so I shouldn't be forced to manually add @nestjs/graphql;

Tried this out with versions: 1.8.2 and 1.8.6 (get same issue for both versions)

For the latest version(1.8.7) I have other problem related with a require that sits somewhere in the code:

/Users/bernardo/Documents/incubeta-repos/creamos-client-management-service/node_modules/nestjs-keycloak-admin/dist/main/service.js:18 const keycloak_admin_client_1 = __importDefault(require("@keycloak/keycloak-admin-client")); ^ Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/bernardo/Documents/incubeta-repos/creamos-client-management-service/node_modules/@keycloak/keycloak-admin-client/lib/index.js from /Users/bernardo/Documents/incubeta-repos/creamos-client-management-service/node_modules/nestjs-keycloak-admin/dist/main/service.js not supported. Instead change the require of index.js in /Users/bernardo/Documents/incubeta-repos/creamos-client-management-service/node_modules/nestjs-keycloak-admin/dist/main/service.js to a dynamic import() which is available in all CommonJS modules.

anonrig commented 2 years ago

@P1K3N due to @keycloak/keycloak-admin-client being a ESM only package after 19.x release, it's required you to use ESM mode with Node.

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

y-sl commented 1 year ago

@anonrig But these links are all 404

anonrig commented 1 year ago

@anonrig But these links are all 404

It seems the keycloak-nodejs-admin-client repository is moved: https://github.com/keycloak/keycloak-nodejs-admin-client