Skip to content
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

Importing Application Insights library in ES6 module import format doesn't work #1311

Closed
c-amunir opened this issue Apr 22, 2024 · 3 comments

Comments

@c-amunir
Copy link

c-amunir commented Apr 22, 2024

I am trying to import application insights into a project that uses ES6.

I have the following code:

import * as appInsights from 'applicationinsights';

const connectionString = '[CONNECTION_STRING]'
appInsights.setup(connectionString).start();
appInsights.defaultClient.trackEvent({
   name: 'test'
});

appInsights.defaultClient.flush();

export default appInsights;

Here's the error I am getting:
TypeError: Cannot read properties of undefined (reading 'trackEvent')

I am using node.js v18.20.2 (LTS).

I tried to use applicationinsights v3.0.0 but I was getting the same issues as Issue 1310

I ended up having to do 2 fixes:

  1. Downgrade to applicationinsights v.2.9.5
  2. Do the following code snippet:
import {createRequire} from 'module';
const require = createRequire(import.meta.url);

let appInsights = require('applicationinsights');
const connectionString = '[CONNECTION_STRING]';
appInsights.setup(connectionString).start();
appInsights.defaultClient.trackEvent({
   name: 'test'
});

appInsights.defaultClient.flush();

export default appInsights;

Optimally, I would be using the ES6 import module and not require, as well as be on v3.0.0 of Application Insights. Though being on v2.9.5 is okay as well for the time being while other issues get resolved.

Any guidance would be much appreciated.

Thanks

@JacksonWeber
Copy link
Contributor

@c-amunir Thank you for bringing this to my attention. I've tested the 3.0.1 release with import syntax and it should work. Please give the updated package a try and let me know. Thank you!

@c-amunir
Copy link
Author

Hello @JacksonWeber,

Thanks for the prompt response. the 3.0.1 release did fix the issue with the import syntax.

I appreciate the prompt response!

@JacksonWeber
Copy link
Contributor

Closing as the issue was resolved with the latest release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants