question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: TS compilation error because of missing URL type

See original GitHub issue

When using applicationinsights latest package ( 2.1.4 ) in TS projects which target Node only (no browser), I get the following compilation error:

node_modules/applicationinsights/out/Declarations/Contracts/TelemetryTypes/NodeHttpDependencyTelemetry.d.ts:12:23 - error TS2304: Cannot find name 'URL'.

12     options: string | URL | http.RequestOptions | https.RequestOptions;
                         ~~~

Probably you guys are missing some module import?

Very dirty workaround: I found similar error (in different NPM package): https://github.com/DefinitelyTyped/DefinitelyTyped/issues/19799 . It suggested adding dom library as workaround. Of course, doing this for your Node project has all kinds of nasty implications, which is why I just downgraded to 1.8.10.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jasonzuvela-wywmcommented, Aug 27, 2021

I resolved this for myself by adding DOM to the tsconfig lib array:

tsconfig.json

{
  "compilerOptions": {
    ...
    "lib": [
      "esnext",
      "DOM" <-- this
    ],
}

It then compiled fine.

1reaction
stazzcommented, Oct 22, 2021

Long time no see! My vacation started pretty quickly after my last comment, and then I got swamped with other things. But, I found out solution that doesn’t need PR to this repo.

If you are running backend stuff, and definitely don’t want to include DOM TS library (to avoid runtime crashes which pass TS typechecks), another option is to use approach explained in @microsoft/microsoft-graph-client library:

First, install some fetch library for Node, e.g. cross-fetch:

  "dependencies": {
    "applicationinsights": "2.1.8",
    "cross-fetch": "3.1.4"
  },

Then, in your TS code, just import fetch library stuff before using "applicationinsights":

// As per instructions from https://docs.microsoft.com/en-us/azure/azure-monitor/app/nodejs
// Load the Application Insights library ,require("applicationinsights"), as early as possible in your scripts before loading other packages.
import "cross-fetch";
import "cross-fetch/polyfill";
import * as appInsights from "applicationinsights";
import { env } from "process";
const instrumentationKey = env.APPINSIGHTS_INSTRUMENTATIONKEY;
if (instrumentationKey) {
  appInsights
    .setup(instrumentationKey)
    .setSendLiveMetrics(true)
    .setAutoCollectConsole(true, true)
    .setAutoDependencyCorrelation(true)
    .start();
}

Now, the tsc will succeed without errors (make sure to have moduleResolution compiler config field set to node), and without DOM library:

$ npm run tsc

> your-project@1.0.0 tsc
> tsc --project tsconfig.json

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript module not found error after compilation
I ran into this problem when compiling TS to ES modules with Node 15. Turns out that TS does not add the ".js"...
Read more >
TypeScript errors and how to fix them
ts ' cannot be compiled under '–isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export...
Read more >
Bug listing with status RESOLVED with resolution TEST ...
... Bug:72974 - "nvidia-kernel fails to compile, because of multiple problems in linux-info.eclass" status:RESOLVED resolution:TEST-REQUEST severity:normal ...
Read more >
Error Messages | Cypress Documentation
This message means that Cypress encountered an error when compiling and/or bundling your test file. Cypress automatically compiles and bundles your test code...
Read more >
Troubleshooting Common Errors - Gatsby
Field "image" must not have a selection since type "String" has no subfields; Problems installing sharp with gatsby-plugin-sharp - gyp ERR! build error...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found