authentik icon indicating copy to clipboard operation
authentik copied to clipboard

NPM package "@goauthentik/api" can not be imported when compiled with TypeScript

Open rabbitlair opened this issue 9 months ago • 0 comments

Describe the bug When installing the @goauthentik/api package into a TypeScript project, the project can be compiled properly, but when the compiled project is run, there is an error coming from the library saying it can not find a module from the package - see logs section

To Reproduce Steps to reproduce the behavior:

  1. Create a Node 22, TypeScript 5.8.3 project and install the @goauthentik/api package
  2. Create a TS source file, and import any component from the @goauthentik/api module
  3. Compile the TS project
  4. Run the compiled project
  5. See the error

I created a quick demo of this issue in the following repository: https://github.com/rabbitlair/goauthentik-demo

Expected behavior The @goauthentik/api module can be imported properly by compiled TypeScript code

Logs

node:internal/modules/esm/resolve:275
    throw new ERR_MODULE_NOT_FOUND(
          ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/****/node_modules/@goauthentik/api/dist/runtime' imported from /****/node_modules/@goauthentik/api/dist/index.js
    at finalizeResolution (node:internal/modules/esm/resolve:275:11)
    at moduleResolve (node:internal/modules/esm/resolve:860:10)
    at defaultResolve (node:internal/modules/esm/resolve:984:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:780:12)
    at #cachedDefaultResolve (node:internal/modules/esm/loader:704:25)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:687:38)
    at ModuleLoader.getModuleJobForImport (node:internal/modules/esm/loader:305:38)
    at ModuleJob._link (node:internal/modules/esm/module_job:137:49) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///****/node_modules/@goauthentik/api/dist/runtime'
}

Node.js v22.16.0

Version and Deployment (please complete the following information):

  • @goauthentik/api version: 2025.6.0-1749054550
  • TypeScript 5.8.3
  • Node v22.16.0

Additional context

By following the advice I found in TypeScript documentation:

In short, "moduleResolution": "bundler" is infectious, allowing code that only works in bundlers to be produced. Likewise, "moduleResolution": "nodenext" is only checking that the output works in Node.js, but in most cases, module code that works in Node.js will work in other runtimes and in bundlers.

I have been able to resolve the issue by changing the following lines in the tsconfig.json file from the @goauthentik/api module, then recompiling it with npm run build:

index a7fe768..1226648 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -9,8 +9,8 @@
         "strict": true,
         "newLine": "lf",
         "target": "ESNext",
-        "module": "ESNext",
-        "moduleResolution": "bundler",
+        "module": "NodeNext",
+        "moduleResolution": "NodeNext",
         "outDir": "dist",
         "skipDefaultLibCheck": true,
         "skipLibCheck": true,

rabbitlair avatar Jun 06 '25 16:06 rabbitlair