javascript icon indicating copy to clipboard operation
javascript copied to clipboard

Upgrade to 0.22.2 fails due to use of new major version of client-node.

Open TonyJDavies opened this issue 2 years ago • 24 comments

Describe the bug When upgraded to latest version 0.22.2 of this library getting the following error:

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/app/node_modules/openid-client/build/index.js from /home/app/node_modules/@kubernetes/client-node/dist/oidc_auth.js not supported.
Instead change the require of index.js in /home/app/node_modules/@kubernetes/client-node/dist/oidc_auth.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/home/app/node_modules/@kubernetes/client-node/dist/oidc_auth.js:5:35)
    at /home/app/node_modules/@kubernetes/client-node/dist/oidc_auth_delayed.js:44:70
    at async DelayedOpenIDConnectAuth.applyAuthentication (/home/app/node_modules/@kubernetes/client-node/dist/oidc_auth_delayed.js:44:22)
    at async KubeConfig.apply(--- cut ---)
    at async KubeConfig.applyOptions (/home/app/node_modules/@kubernetes/client-node/dist/config.js:388:9)
    at async KubeConfig.applyToRequest (/home/app/node_modules/@kubernetes/client-node/dist/config.js:110:9)

This appears to be due to using the new version of client-node. ** Client Version ** 0.22.2

** Server Version ** e.g. 1.19.1

To Reproduce Steps to reproduce the behavior:

Expected behavior A clear and concise description of what you expected to happen.

** Example Code** Code snippet for what you are doing

Environment (please complete the following information):

  • OS: [e.g. Windows, Linux]
  • NodeJS Version [eg. 10]
  • Cloud runtime [e.g. Azure Functions, Lambda]

Additional context Add any other context about the problem here.

TonyJDavies avatar Nov 04 '24 13:11 TonyJDavies

In order to make this library work with Jest, I added this transform logic to the jest.config.js file that ts-jest was using:

  transform: {
    "node_modules/@kubernetes/client-node/dist/index.js": [
      "ts-jest",
      {
        tsconfig: "tsconfig.test.json",
        isolatedModules: true
      }
    ],
    "node_modules/openid-client/build/index.js": [
      "ts-jest",
      {
        tsconfig: "tsconfig.test.json",
        isolatedModules: true
      }
    ],
    "node_modules/oauth4webapi": [
      "ts-jest",
      {
        tsconfig: "tsconfig.test.json",
        isolatedModules: true
      }
    ],
    "^.+\\.(t|j)s$": [
      "ts-jest",
      {
        tsconfig: "tsconfig.test.json",
        isolatedModules: true
      }
    ]
  },
  transformIgnorePatterns: [
    "/node_modules/(?!(@kubernetes/client-node|openid-client|oauth4webapi)/)"
  ]
};

and I added a new tsconfig file specifically for the tests:

{
  "compilerOptions": {
    "allowJs": true
  },
  "extends": "./tsconfig.json"
}

Then I updated the tests to look for tests files in the directory where the Typescript files for the tests were located instead of in the directory where the compiled JS files were located.

christhegrand avatar Apr 30 '25 18:04 christhegrand

@christhegrand Thanks a lot for sending your configuration. It did not work for me out of the box - I assume because we were using different versions of the library. Here is a summary

  • install the newest version of jest (for me it only works with 29.3.2 but not with 29.3.1)
  • the jose library also needs to be excluded
  • there is no need for a transform ts-jest configuration per ESM node_module. Excluding the libraries in transformIgnorePatterns is enough.
  • tsconfig.json needs to contain "esModuleInterop": true

So this is the jest.config.js:

module.exports = {
  testEnvironment: 'node',
  preset: 'ts-jest',
  moduleFileExtensions: ['js', 'json', 'ts', 'tsx', 'jsx', 'node'],
  transform: {
    // (Added support for .tsx files but not required)
    '^.+\\.(t|j)sx?$': [
      'ts-jest',
      {
        tsconfig: 'tsconfig.test.json',
      },
    ],
    // (No need for module specific ts-jest configurations)...
  },
  transformIgnorePatterns: [
    // Added jose library (probably this ESM dependency was added in the newer versions)
    // This will work for @kuberenets/client-node version 1.1.2
    '/node_modules/(?!(@kubernetes/client-node|openid-client|oauth4webapi|jose)/)',
  ],
};

This is the tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs" /* Specify what module code is generated. */,

    // THIS IS IMPORTANT!
    "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
  },
  "include": [
    "src/**/*" // Include all files in the 'src' folder
  ]
}

And this is tsconfig.test.json (unchanged):

{
  "compilerOptions": {
    "allowJs": true
  },
  "extends": "./tsconfig.json"
}

Finally, you can import the library like this:

import * as k8s from '@kubernetes/client-node';

I also set up a stackblitz with a minimal sample which works for @kuberenets/client-node version 1.1.2 (run npm run test): https://stackblitz.com/edit/stackblitz-starters-un7zge3j?file=jest.config.js

philippkolbe avatar May 01 '25 21:05 philippkolbe

This change is causing problems for me. The library doesn't work out of the box anymore when imported from within a NextJS application, and I can't get it to work with Jest either.

@christhegrand Did you get this issue resolved with your nextjs app or just the jest part? I am having trouble with the client version 1.0.0+ working within my nextjs app.

saulrobe avatar Oct 06 '25 18:10 saulrobe

I can't remember now how I addressed the issues with importing from Next. We've since modified our code so we don't need to use this library at all and are about to remove the dependency.

christhegrand avatar Oct 06 '25 18:10 christhegrand