-
Notifications
You must be signed in to change notification settings - Fork 140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
aplicationinsights package incompatibilities with ESM #1354
Comments
@thekip I also found that applicationinsights v3 doesn't work well with ESM. For your issue, I found the following code works for me, the import applicationInsights from "applicationinsights";
applicationInsights.setup(connectionString)
.setSendLiveMetrics(true)
.start();
console.log(applicationInsights.defaultClient === undefined) // defaultClient won't be undefined
applicationInsights.defaultClient.trackEvent({name: "TestEvent"}); // this line works but it will give me some warning/error log But I will get the following log which is caused by the
I can get the custom event in the application insights, but this log seems a bug to me and it's quite annoying. I am using version 3.2.2. |
@zhiyuanliang-ms Unfortunately that log is something that OpenTelemetry throws when we initialize before letting the resource detector's attempt to determine if the SDK is running in a VM. I'll look into how to resolve that log, however it should have no impact on your ability to receive telemetry. |
@thekip Solution to this issue presented by the OTel community is in the discussion on #1375 just FYI in case you run into issues with automatically collecting telemetry from HTTP, DBs, etc. |
Describe the bug
applicationinsights
doesn't work properly in ESM environmentThis issue is possibly a duplicate of #1205 but with more details and workaround.
To Reproduce
Steps to reproduce the behavior:
"type": "module",
inpackage.json
defaultClient
would be undefined.Expected behavior
defaultClient
should be fulfilledAdditional context
This happened because of the differences how native ESM and transpiled to CJS modules works.
If you look to the entry point of the
'applicationinsights'
package you will see:When this package would be requested in CJS context, those getters would be invoked only when user's code will actually access them, but in ESM context, ESM loader enumerate all properties which are exported from the package and save result internally, so getter for
defaultClient
is executed only once and before this client ever initialized.There is no simple way to fix this, other than don't use late-binding at all. So instead of
defaultClient
expose a functiongetDefaultClient()
To workaround the issue, i created a cjs file which is called from my ESM modules with the following content:
The text was updated successfully, but these errors were encountered: