peerjs icon indicating copy to clipboard operation
peerjs copied to clipboard

Cannot import from typescript

Open adadgio opened this issue 7 years ago • 7 comments

Hello, despite looking at similar issues "apparently" resovled, typescript imports are broken:

  • (Docs) import Peer from 'peerjs' --> has not default export
  • import { Peer } from 'peerjs' --> has no exported member Peer
  • import * as peerjs from 'peerjs' --> no TS error but at runntime, new Peer is not a function (its an object...)

Is there any quick fix to this, its really sad! :/

adadgio avatar Oct 21 '19 08:10 adadgio

I am having the same issue! Thanks!

therealtimhawkins avatar Oct 29 '19 13:10 therealtimhawkins

Should works

import Peer from 'peerjs';

console.log(new Peer())

My tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react",
    "types": [
      "node"
    ],
  },
  "include": [
    "src"
  ]
}

afrokick avatar Oct 29 '19 18:10 afrokick

@caribeedu share your tsconfig please

afrokick avatar Nov 06 '19 12:11 afrokick

This issue is preventing my team from using this library with Angular.

Is there any alternative way for us to import peerjs while allowing us to have types?

m-thompson-code avatar Nov 25 '20 02:11 m-thompson-code

it's working just add the library and import it like this

import Peer from 'peerjs';

louayrm12 avatar Nov 25 '20 10:11 louayrm12

By adding esModuleInterop to the project's tsconfig.ts, we were able to successfully use peerjs and make a build with Angular 11:

{
    // ...
    "compilerOptions": {
        // ...
        "esModuleInterop": true, // <-- add this
    }
}

By doing this, import Peer from 'peerjs'; works. We do get the warning: "Critical dependency: the request of a dependency is an expression" whenever we serve/build, but I haven't noticed anything odd yet. Also this warning isn't critical as mentioned here: peerjs FAQ.

Hopefully this solution works for you @adadgio

m-thompson-code avatar Dec 04 '20 20:12 m-thompson-code

fixed in peerjs@beta

afrokick avatar Feb 20 '22 02:02 afrokick