cloud-health-connect icon indicating copy to clipboard operation
cloud-health-connect copied to clipboard

Can't import module from typescript

Open kuka-radovan opened this issue 5 years ago • 4 comments

I wanted to import this module to my typescript code. I tried to do it as follows:

import * as health from "@cloudnative/health-connect";

or I even tried

import health from "@cloudnative/health-connect";

But I cannot reach exported classes (e.g. HealthChecker). I am getting undefined from calling health.HealthChecker from first option or Module "@cloudnative/health-connect/index" has no default export. in case of second option.

The only solution I found is to import it like this

import { default as health } from "@cloudnative/health-connect";

but I personally don't like it and don't understand, why I have to rename default export that looks like it doesn't even has.

Do you have any solution or recommendation how to use this package in typescript? I am using typescript v 3.6.3.

kuka-radovan avatar Sep 19 '19 14:09 kuka-radovan

Hi @kuka-radovan there has been a new release recently and using the sample code below I was able to import the health-connect module using

 import * as health from '@cloudnative/health-connect'

and it seems to be working. My sample express app using health-connect in typescript is below. Let me know if you still encounter problems.

import express from 'express';
import * as health from '@cloudnative/health-connect';

const port = 3000;

const app = express();
let healthCheck = new health.HealthChecker();

const livePromise = () => new Promise<void>((resolve, _reject) => {
  setTimeout(resolve, 100, 'foo');
});

let liveCheck = new health.LivenessCheck("liveCheck", livePromise);
healthCheck.registerLivenessCheck(liveCheck);


app.use('/live', health.LivenessEndpoint(healthCheck))
app.get('/', (req, res) => {
  res.send('Hello!');
});
app.listen(port, () => {
  console.log("Listening on 3000");
});

andrewhughes101 avatar Sep 26 '19 15:09 andrewhughes101

Hi @andrewhughes101, thank you for your reply. I have the same result also with the new version. Please, can you provide your tsconfig.json file for that example? Thank you.

kuka-radovan avatar Sep 30 '19 14:09 kuka-radovan

Hi I'm using the same tsconfig.json as the cloud-health-connect module found here

andrewhughes101 avatar Sep 30 '19 14:09 andrewhughes101

I don't understand, why I am getting health.HealthChecker is not a constructor error... My tsconfig.json looks the same. I have specific situation with that, I am trying to use that health-connect in library written in typescript, and after that I am using that lib in js code. TSC compiles without problems but node.js code throws that error when starting.

kuka-radovan avatar Sep 30 '19 14:09 kuka-radovan