gajus / lightship

Abstracts readiness, liveness and startup checks and graceful shutdown of Node.js services running in Kubernetes.
Other
515 stars 33 forks source link

after 8.0.0 not possible to use in CJS project #64

Closed swarnadeepsaha closed 1 year ago

swarnadeepsaha commented 1 year ago

after 8.0.0 is not able to use in the CJS project reason it has no export definition for CJS require. not able to move beyond 7.

Issue: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /usr/app/bin/node_modules/lightship/package.json

Please add a build for CJS too and redefine the export similarly to

"exports": {
    "require": "./cjs/index.js",
    "import": "./esm/index.js"
  },
dsstewa commented 1 year ago

Also having this issue

tdeekens commented 1 year ago

Same here.

throrin19 commented 1 year ago

same problem here

gajus commented 1 year ago

fixed in v9.0.1

throrin19 commented 1 year ago

Same problem :

Error [ERR_REQUIRE_ESM]: require() of ES Module xxx/nodejs/kube-collector/node_modules/lightship/dist/cjs/index.js from xxx/xxx/xxx.js not supported.
Instead change the require of index.js in xxx/xxx/xxx.js to a dynamic import() which is available in all CommonJS modules.

Edit, I fix the problem :

In 7.2, I have this code :

const { createLightship } = require('lightship');

module.exports = async () => {
    const lightship = await createLightship({
        detectKubernetes        : process.env.NODE_ENV === 'production',
        // gracefulShutdownTimeout : 1000 * 60,
        // shutdownHandlerTimeout  : 1000 * 60,
    });

    /* istanbul ignore next */
    lightship.queueBlockingTask(new Promise((resolve) => {
        setTimeout(() => {
            // Lightship service status will be `SERVER_IS_NOT_READY` until all promises
            // submitted to `queueBlockingTask` are resolved.
            resolve();
        }, 1000);
    }));

    return lightship;
};

In 9.0.1 I change to that, and it works fine :

module.exports = async () => {
    const { createLightship } = await import('lightship');

    const lightship = await createLightship({
        detectKubernetes        : process.env.NODE_ENV === 'production',
        // gracefulShutdownTimeout : 1000 * 60,
        // shutdownHandlerTimeout  : 1000 * 60,
    });

    /* istanbul ignore next */
    lightship.queueBlockingTask(new Promise((resolve) => {
        setTimeout(() => {
            // Lightship service status will be `SERVER_IS_NOT_READY` until all promises
            // submitted to `queueBlockingTask` are resolved.
            resolve();
        }, 1000);
    }));

    return lightship;
};

(In my case, I used an abstraction method to simplify usage if lightship have any major change in the future)