ApplicationInsights-JS icon indicating copy to clipboard operation
ApplicationInsights-JS copied to clipboard

Typescript 2.9, ai.module.d.t.s is not a module

Open cmenekse opened this issue 8 years ago • 41 comments

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

cmenekse avatar May 31 '18 21:05 cmenekse

Where are you seeing @ types/applicationinsights-js ?

jpiyali avatar May 31 '18 21:05 jpiyali

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.

cmenekse avatar May 31 '18 22:05 cmenekse

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

jpiyali avatar May 31 '18 22:05 jpiyali

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?

cmenekse avatar May 31 '18 22:05 cmenekse

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.

chrarnoldus avatar Jun 01 '18 08:06 chrarnoldus

@barustum - can you take a look?

jpiyali avatar Jun 01 '18 13:06 jpiyali

Of course.

barustum avatar Jun 01 '18 15:06 barustum

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

barustum avatar Jun 01 '18 23:06 barustum

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.

cmenekse avatar Jun 02 '18 00:06 cmenekse

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)

MadaraUchiha avatar Jun 10 '18 14:06 MadaraUchiha

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.

ppatalong avatar Jun 11 '18 09:06 ppatalong

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

barustum avatar Jun 11 '18 21:06 barustum

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

nithinmohantk avatar Jun 12 '18 14:06 nithinmohantk

@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?

jpiyali avatar Jun 12 '18 16:06 jpiyali

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",

nithinmohantk avatar Jun 13 '18 13:06 nithinmohantk

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 avatar Jun 13 '18 23:06 engineersamuel

@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/@​types/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 avatar Jun 14 '18 13:06 ayrtonmassey

@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.

engineersamuel avatar Jun 14 '18 15:06 engineersamuel

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

oozusta avatar Jun 14 '18 15:06 oozusta

@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/@​types/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 avatar Jun 14 '18 17:06 ayrtonmassey