Vermonster / fhir-kit-client

Node.js FHIR client library
MIT License
173 stars 37 forks source link

Unable to instantiate client with import. #177

Closed JoshWalshaw closed 2 years ago

JoshWalshaw commented 2 years ago

Expected Behavior

Should be able to create a new instance of Client when using ES6 imports when following the documentation to implement the library.

Actual Behavior

Getting the following error when using import Client from 'fhir-kit-client';.

You can however get access to an object when doing const Client = require('fhir-kit-client'); however this doesn't provide any typescript support this way.

web-api   | [Nest] 539  - 05/24/2022, 11:10:40 AM   ERROR [ExceptionsHandler] fhir_kit_client_1.default is not a constructor
web-api   | TypeError: fhir_kit_client_1.default is not a constructor
web-api   |     at new SomeService (/some-service.ts:14:19)
web-api   |     at Injector.instantiateClass (/usr/src/app/node_modules/@nestjs/core/injector/injector.js:301:19)
web-api   |     at callback (/usr/src/app/node_modules/@nestjs/core/injector/injector.js:48:41)
web-api   |     at processTicksAndRejections (node:internal/process/task_queues:96:5)
web-api   |     at Injector.loadInstance (/usr/src/app/node_modules/@nestjs/core/injector/injector.js:52:9)
web-api   |     at Injector.loadProvider (/usr/src/app/node_modules/@nestjs/core/injector/injector.js:74:9)
web-api   |     at Injector.resolveComponentHost (/usr/src/app/node_modules/@nestjs/core/injector/injector.js:164:13)
web-api   |     at async Promise.all (index 0)
web-api   |     at Injector.loadCtorMetadata (/usr/src/app/node_modules/@nestjs/core/injector/injector.js:329:23)
web-api   |     at Injector.resolveConstructorParams (/usr/src/app/node_modules/@nestjs/core/injector/injector.js:89:26)

Steps to Reproduce the Problem

Simplest form would be:

  1. yarn add fhir-kit-client
  2. import Client from 'fhir-kit-client';
    const client = new Client({ baseUrl: 'http://foo.com' });
    console.log('client: ', client);

Specifications

bkaney commented 2 years ago

Hey @JoshWalshaw -- I don't know what your setup is (looks like nestjs), but we use this library with typescript all the time.

See it working here as well: https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAYQDbAKYDt4DMoRHAciwAtgoBaAa2BnIGMUMYCBuAWAChO6J0BneAzSY4AXjjpUAd0SNMACgDeAIwCGfVAFUoSAFyFiMGGF0B6U6rohUAOh4hTJMgQC+ASnZcOQA

Can you push up a git repo reproducing your problem?

JoshWalshaw commented 2 years ago

Hey @bkaney, thanks for the quick response!

I've created a super quick repo which can be found here. All you'd need to do is run yarn start:dev after installing dependencies and you should see the error in the terminal.

For reference, I've also put down the output of my console below too, but it's the same error I'm getting in the actual project I'm trying to implement - only that in the repo linked above I'm throwing it in the bootstrap method so that it attempts to do things on startup as opposed to when hitting an endpoint.

Hopefully this helps, let me know if there's anything else you'd want to know!

[14:19:58] Starting compilation in watch mode...

[14:20:01] Found 0 errors. Watching for file changes.

/Users/joshwalshaw/Documents/Node/fhir/src/main.ts:7
  const client = new Client({ baseUrl: 'http://foo.com' });
                 ^
TypeError: fhir_kit_client_1.default is not a constructor
    at bootstrap (/Users/joshwalshaw/Documents/Node/fhir/src/main.ts:7:18)
    at Object.<anonymous> (/Users/joshwalshaw/Documents/Node/fhir/src/main.ts:13:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47
bkaney commented 2 years ago

Hi @JoshWalshaw - You need to turn on https://www.typescriptlang.org/tsconfig#esModuleInterop in your tsconfig.json. Once enabled, everything is fine.

image

Right now, the library is intentionally plain vanilla JS (requiring this setting)

JoshWalshaw commented 2 years ago

Amazing! I added that into my tsconfig.json and it's working fine now, thanks a bunch for this - much appreciated.

Might be worth adding something in the ReadMe if this is a requirement so similar issues in the future don't get created, If you'd like I can fork and make a PR with something in?