62

I am getting File node_modules/@types/webrtc/index.d.ts is not a module with this code:

import * as webrtc from "webrtc";
const peerConnection1 = new RTCPeerConnection();

I have installed the typings using npm i @types/webrtc --save-dev. Hovering over RTCPeerConnection in const peerConnection1 = new RTCPeerConnection(); display type annotations in Visual Studio Code so at least the code editor sees the types. Running tsc (or webpack with ts-loader) fails with the error.

I have tried npm i webrtc --save in a misguided attempt for solving this, but it did not change anything and I really only want the typings anyway, WebRTC is right there in the browser, I don't need a package for that. (Support aside.)

The index.d.ts file indeed is not a module, it just references two other files with interfaces in them. So I thought to remove import * as webrtc from "webrtc"; hoping the typings will still be visible by tsc somehow. (But that's impossible since I exclude node_modules in TypeScript config file.) When I do that RTCPeerConnection is no longer recognized.

Adding /// <reference src="node_modules/@types/webrtc/" /> did not help, tsc says Invalid reference directive syntax.

You can view a repository with minimal repro here on GitLab. I am not too well versed in TypeScript typings acquisition so please forgive my ignorance if I'm going about this all wrong.

Tomáš Hübelbauer
  • 5,273
  • 8
  • 46
  • 86

7 Answers7

66

webrtc is part of the browser; you're trying to import a module. Simply import the (typings) library:

import "webrtc";

you may need to use "moduleResolution": "node" in the compiler options.

Alternatively use the "types": ["webrtc"] compiler option and the compiler will automatically load those types up for you.

Meirion Hughes
  • 21,021
  • 8
  • 65
  • 114
10

You probably want to add

"types": ["webrtc"]

to your tsconfig.json, or less preferrably, to use

/// <reference types="webrtc" />

in your source files. Here's an example of it in your tsconfig.json:

{
    "compilerOptions": {
        "target": "es5",
        "sourceMap": true,
        "noImplicitAny": true,

        "types": ["webrtc"]
    },
    "exclude": [
        "node_modules"
    ]
}

This tells TypeScript it should include webrtc declarations in your build

Daniel Rosenwasser
  • 15,697
  • 7
  • 40
  • 57
  • 2
    Don't set `types` or else *only* those types will get loaded — usually everything in `node_modules/@types` is loaded, and this will prevent that behaviour. You simply need to `npm i @types/webrtc` these days. – aendrew Mar 21 '18 at 14:15
  • @aendrew that isn't entirely true because it depends on where your `tsconfig.json` is placed relative to your `node_modules` directory. – Daniel Rosenwasser Mar 21 '18 at 23:26
7

Another option is to add a new declaration file *.d.ts in your module, i.e.:

declare module 'my-module';
6

No need to import anything, run following:

  1. npm install --save @types/webrtc
  2. update tsconfig.json -

    "types": [ "@types/webrtc" ]

Sabir Hussain
  • 93
  • 2
  • 5
1

/// <reference types="@types/<your_types_module>" />

You may or may not want to do this depending on your build and style needs, but this seems to be the quick (and dirty) fix.

youngrrrr
  • 2,285
  • 3
  • 20
  • 39
0

use require

const webRtc = require('webrtc');

and you import should be good to go

0

In my case it was a corrupted dependency, deleting the module and npm install did the work.