123

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.

11 Answers 11

174

In vscode

Ctrl + Shift + p > Developer: Reload Window.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for the answer! Why does this work and restarting vsCode does not?
Wow. I was having fits with VS Code giving this error about the React index.d.ts when my app was compiling just fine. Restarts of VS Code had no effect but this did. Wish I knew why.
91

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.

Comments

18

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

3 Comments

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 that isn't entirely true because it depends on where your tsconfig.json is placed relative to your node_modules directory.
Yeah this just makes TS not load any other types, even for my local app modules it now spews out thousands of errors about missing types...
11

No need to import anything, run following:

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

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

2 Comments

This will make it so that only the webrtc types are imported. Don't update tsconfig.json.
npm install --save-dev @types/... is preferred - normally, TS is not used in production
8

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

declare module 'my-module';

Comments

6

In my case, I was getting this error message with an index.d.ts file generated by the TypeScript compiler.

The problem was that I’d forgotten to specify any import or export declarations in the source .ts file. I removed the original module.export = {…} bit when porting from .js to .ts, and hadn’t yet replaced it.

This is how the docs define a ‘module’ (emphasis added):

In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module. Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well).

Adding an export to the things I intended to export turned the index.d.ts into a module, clearing the error.

1 Comment

Mine was related in that the generated file didn't have an export, but my issue was because I was using outFile with a ESM (NodeNext) module type. Switching to outDir fixed things for my issue.
4

I was getting similar error on VS code editor. Simply, closing and opening VSCode again solved the problem for me.

2 Comments

cant belive I didnt think of this (facepalm)
Welp, this fixed it for me too
2

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

Comments

1

I got this error because I forked a library, renamed it in package.json but didn't rename this line in its .d.ts file (the file pointed to by "types": in package.json):

declare module "package-name" {

to

declare module "my-forked-package-name" {

Comments

0

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

1 Comment

This unfortunately could be a solution sometimes
-6

use require

const webRtc = require('webrtc');

and you import should be good to go

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.