Applicationinsights-js: Typescript 2.9, ai.module.d.t.s is not a module

Created on 31 May 2018  路  40Comments  路  Source: microsoft/ApplicationInsights-JS

When trying to use it with typescript I am seeing ai.module.d.ts not a module error when using both. @types/applicationinsights-js and ai.module.d.ts
If I do not install @types/applicationinsights-js ts tells me to install it saying type definitions not found
If I install @types/applicationinsights-js and delete ai.module.d.ts everything works.

I was wondering why that happens and why there are multiple type definitions?

Thank you

Most helpful comment

Folks, I published a new NPM package with a beta tag. The package simply does not include ai.module.d.ts. Please give it a try and let me know if that works for you without any hacks.

npm install applicationinsights-js@beta

All 40 comments

Where are you seeing @ types/applicationinsights-js ?

When I do not install @types/applicationinsights-js
I get ai.module.d.ts is not a module during build and when hovered on the error in vscode it says:

Could not find a declaration file for module 'applicationInsights.js' ai.module.js implicitly has an 'any' type try npm install @types/applicationinsights-js

Additionally when both bundle/ai.module.d.ts and @types/applicationinsights-js is there/installed it does not build in recently updated typescript 2.9.0 .

However it does build fine in typescript 2.8.3 although not sure how these are related after I skimmed through the changelogs of typescript.

Are you trying to consume ai.module.d.ts in a commonjs typescript project?

yes , I am trying to use it with typescript and it is a commonjs typescript project. Ah maybe is it an issue with import/exports with commonJS vs AMD etc?

We're receiving the same error message when trying to upgrade from TypeScript 2.8.4 to 2.9.1 (in 2.8.4 it works fine). We're importing Application Insights like so:

import { AppInsights } from 'applicationinsights-js';

and the exact error message we're seeing is:

TS2306: File 'C:/blablabla/node_modules/applicationinsights-js/bundle/ai.module.d.ts' is not a module.

package.json has version "applicationinsights-js": "1.0.17". The module style in tsconfig.json is set to commonjs.

@barustum - can you take a look?

Of course.

possibly related to this: https://github.com/Microsoft/TypeScript/issues/24556

Although not ideal, but can you try add the following line to node_modules/applicationinsights-js/package.json:

"types": "../node_modules/@types/applicationinsights-js/index.d.ts"

If that works, it's a work around until we publish a new SDK. Or possible go back to ts 2.8.4

yes, adding that seems to fix the error.
out of curiosity though why is there two separate .d.ts files ?

Also I believe adding just "types": "../node_modules/@types/applicationinsights-js/index.d.ts" might break things if @types of it is not installed since it is on a separate npm package that is not necessarily installed with applicationinsights-js.

This is affecting us too. Adding stuff in the node_modules directory is not possible for us.

How long do you figure until there's a fix for this? (I'm willing to contribute a PR if it would help)

Same here - we use AI.JS in our core project, therefore all nightly builds failed.
We used band-aid fix:

declare module "applicationinsights-js"; in typings.d.ts.

@Dmitry-Matveev @SergeyKanzhelev Do you guys know why we have both type definition files?

I am still having the issue on the CI Server. Any help is really appreciated.

@nithinmohantk - we are looking at options. 2.9.1 is not the version that everyone is on. Can you switch to 2.8.3 until a update is avl?

I explicitly added this in my ### package.json, to restrict accidentally restoring 2.9.1 in CI server :
"typescript": ">=2.6.2 <2.8.3",

Here is a workaround for 2.9. Make sure you have a custom typings file, for example in your tsconfig.json

{
  ...
  "compilerOptions": {
    ...
    "typeRoots": ["@types", ...]
    ...
  }
}

Then in your <project>/@types/custom.d.ts:

declare module 'applicationinsights-js' {
    const AppInsights: Microsoft.ApplicationInsights.IAppInsights;
}

And voila, the import works and is typed: import { AppInsights } from 'applicationinsights-js';

@engineersamuel that didn't work for me - perhaps I'm doing it wrong?

package.json

...
"dependencies": {
    "applicationinsights-js": "1.0.18",
    ...
},
"devDependencies": {
    "@types/applicationinsights-js": "1.0.5",
    "typescript": "2.9.1",
    ...
}
...

tsconfig.json

...
"typeRoots": [
    "./src/@types", 
    "./node_modules/@types"
],
...

src/@鈥媡ypes/custom.d.ts

declare module 'applicationinsights-js' {
    const AppInsights: Microsoft.ApplicationInsights.IAppInsights;
}

I'm not sure how this would solve the issue as @types/applicationinsights-js already contains this definition.

The solution posted by @cmenekse did work:

package.json

...
"dependencies": {
    "applicationinsights-js": "1.0.18",
    ...
},
"devDependencies": {
    "@types/applicationinsights-js": "1.0.5",
    "typescript": "2.9.1",
    ...
}
"scripts": {
    "postinstall": "del .\\node_modules\\applicationinsights-js\\bundle\\ai.module.d.ts",
    ...
}
...

@ayrtonmassey Remove the @types/applicationinsights-js as the ai.module.d.ts is already provided in applicationinsights-js. Make sure you don't have that postinstall script in there either.

I haven't dug too much in-depth on the why here, only to surmise there is conflict in reading the import vs the type defs. Re-declaring the module with applicationinsights-js overrides any node_module definition and therefore works around the issue with the package itself. And it appears that the Microsoft.ApplicationInsights.IAppInsights is already visible. This is evidenced by the fact that if you comment out that custom declare module but still import { AppInsights } from 'applicationinsights-js' then you can do the following:

const x: Microsoft.ApplicationInsights.IAppInsights = null;

Even despite the error being present on the import. That means the type is being read, but there is an issue with how the package is configured with 2.9, hence the requirement to re-declare to override.

"downgrade from latest version of another package because nobody will fix this package". good advice.

@engineersamuel I tried with the following:

package.json

...
"dependencies": {
    "applicationinsights-js": "1.0.18",
    ...
},
"devDependencies": {
    "typescript": "2.9.1",
    ...
}
...

tsconfig.json

...
"typeRoots": [
    "./src/@types", 
    "./node_modules/@types"
],
...

src/@鈥媡ypes/custom.d.ts

declare module 'applicationinsights-js' {
    const AppInsights: Microsoft.ApplicationInsights.IAppInsights;
}

i.e. without @types/applicationinsights-js, but it still didn't work for me.

The solution I posted above (install @types/applicationinsights-js, use a postinstall hook to remove ai.module.d.ts) worked for me.

@ayrtonmassey change your dev dependency to:

"devDependencies": { "typescript": ">=2.6.2 <2.8.5", ... }

Then do an NPM install again and try.

We know it works for 2.8.X. The issue is that the included typings break our builds in 2.9.X.

Thank you for letting us know that we have the option to downgrade.

Can the applicationinsights-js developers take a look at this for those of us who want to continue using TS 2.9.X? This is the only module I'm experiencing this issue with.

This GitHub issue has been created to track the issue, so that the applicationinsights-js team is aware there is a problem and to notify users when the issue is fixed. We have a workaround, there is no urgent need to fix the problem but there is a desire from the community to see it fixed.

Please keep any further discussion on topic.

One of the solutions proposed for this involve not generating the ai.module.d.ts. I am curious does anyone take a dependency on this file by using the reference tag as such?
///// <reference path="./node_modules/applicationinsights-js/bundle/ai.module.d.ts" />

FYI for all. We decided to stop generating the ai.module.d.ts, and let developers depend on the @types package. I am making the fix this week. Thanks

Folks, I published a new NPM package with a beta tag. The package simply does not include ai.module.d.ts. Please give it a try and let me know if that works for you without any hacks.

npm install applicationinsights-js@beta

@barustum the beta package + @types/applicationinsights-js works for me without any hacks. Thanks!

I'm having the same issue after upgrading my Angular (CLI) project to [email protected].
node_modules/applicationinsights-js/bundle/ai.module.d.ts' is not a module.

 - [email protected]
 - @types/[email protected]

Based on your suggestions, I've tried the following :

  • Removing the @types/[email protected] didn't solve the problem 馃憥
  • Upgrading to the applicationinsights-js to the 1.0.19-beta.1 solved it 馃憤. At this point you can remove the typings package @types/applicationinsights-js

it worked for me too, thank you!

Is there an ETA on a production release for this fix?

We plan to have a new SDK release next week. I'll update the thread as soon as it is released.

That new beta package worked great, thanks for the interim fix.

"applicationinsights-js": "^1.0.19-beta.1", and "@types/applicationinsights-js": "^1.0.5" worked for us! thanks!

Like @baywet, I still needed the ref to "@types/applicationinsights-js": "^1.0.5". I didn't investigate why.

@barustum any update on when the new SDK might be released?

Hi @praneetloke. We deployed to our pre-production on Monday, and today we're ready to deploy to real production. I'll update the thread as soon as we are deployed.

Deployment is complete. latest applicationinsights-js package should contain the fix. Thanks.

Updated to "applicationinsights-js": "1.0.19", "@types/applicationinsights-js": "1.0.6". Works perfectly, thanks!

Thanks @ayrtonmassey for confirming.

its work fine

npm install --save-dev @types/googlemaps
At the beggining of your component file, type:
///

Was this page helpful?
0 / 5 - 0 ratings