Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to import Peer 1.0.1 in Angular 8 app #552

Closed
furozen opened this issue Jul 10, 2019 · 15 comments
Closed

Unable to import Peer 1.0.1 in Angular 8 app #552

furozen opened this issue Jul 10, 2019 · 15 comments
Labels
Milestone

Comments

@furozen
Copy link

furozen commented Jul 10, 2019

Hello!
I tried to use peerJs with angular 8 but failed.
I did not able to import it to an app.

I've created a tiny project ( https://github.com/furozen/PeerJs-example )for show this issue:

#1

import * as Peer from 'peerjs';

compile error:

ERROR in src/app/app.component.ts:14:16 - error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.

14     let peer = new Peer();
                  ~~~~~~~~~~

  src/app/app.component.ts:3:1
    3 import * as Peer from 'peerjs';
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Type originates at this import. A namespace-style import cannot be called or constructed, and will cause a failure at runtime. Consider using a default import or import require here instead.

#2

import { Peer } from 'peerjs'

compile error:

ERROR in src/app/app.component.ts:3:10 - error TS2305: Module '"../../node_modules/peerjs"' has no exported member 'Peer'.

3 import { Peer } from 'peerjs'
         ~~~~

#3

import Peer from 'peerjs'

compile error:

WARNING in ./node_modules/peerjs/dist/peerjs.min.js 1:292-296
Critical dependency: the request of a dependency is an expression

runtime:

Uncaught ReferenceError: parcelRequire is not defined
    at push../node_modules/peerjs/dist/peerjs.min.js.parcelRequire.vHo1 (peerjs.min.js:1)
    at Object../node_modules/peerjs/dist/peerjs.min.js (peerjs.min.js:1)
    at __webpack_require__ (bootstrap:79)
    at Module../src/app/app.component.ts (main.js:112)
    at __webpack_require__ (bootstrap:79)
    at Module../src/app/app.module.ts (app.component.ts:11)
    at __webpack_require__ (bootstrap:79)
    at Module../src/main.ts (main.ts:1)
    at __webpack_require__ (bootstrap:79)
    at Object.0 (main.ts:12)

#4

import Peer = require('peerjs');

compile error:


    ERROR in src/app/app.component.ts(3,1): error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead

runtime:

AppComponent_Host.ngfactory.js? [sm]:1 ERROR ReferenceError: Peer is not defined
    at new AppComponent (app.component.ts:15)
    at createClass (core.js:27863)
    at createDirectiveInstance (core.js:27685)
    at createViewNodes (core.js:38315)
    at createRootView (core.js:38187)
    at callWithDebugContext (core.js:39716)
    at Object.debugCreateRootView [as createRootView] (core.js:38953)
    at ComponentFactory_.create (core.js:26827)
    at ComponentFactoryBoundToModule.create (core.js:22791)
    at ApplicationRef.bootstrap (core.js:35343)
@furozen
Copy link
Author

furozen commented Jul 10, 2019

Changing module to "CommonJs" helps remove compilation error but do not fix the runtime error: still #3 case.

the

ReferenceError: parcelRequire is not defined

can be linked with this issue: parcel-bundler/parcel#1401

@kidandcat
Copy link
Member

The right way is import Peer from "peerjs", what you are getting is a webpack warning:

WARNING in ./node_modules/peerjs/dist/peerjs.min.js 1:292-296
Critical dependency: the request of a dependency is an expression

This shouldn't affect the library nor the compile process.

parcel-bundler/parcel#2883

@furozen
Copy link
Author

furozen commented Jul 11, 2019

The right way is import Peer from "peerjs", what you are getting is a webpack warning:

WARNING in ./node_modules/peerjs/dist/peerjs.min.js 1:292-296
Critical dependency: the request of a dependency is an expression

This shouldn't affect the library nor the compile process.

parcel-bundler/parcel#2883

Yep, it is working approach thanks.
To conclude:
you should have "esModuleInterop": true, in tsconfig.json
and import lib as : import Peer from "peerjs"

@furozen furozen closed this as completed Jul 11, 2019
@furozen furozen reopened this Jul 12, 2019
@furozen
Copy link
Author

furozen commented Jul 12, 2019

sorry! it was patch in index.html which I added and forgot to remove:

  <script>
    window.global  = window;
    var parcelRequire;
  </script>

without this patch I got :

Uncaught ReferenceError: parcelRequire is not defined
    at push../node_modules/peerjs/dist/peerjs.min.js.parcelRequire.vHo1 (peerjs.min.js:1)
    at Object../node_modules/peerjs/dist/peerjs.min.js (peerjs.min.js:1)
    at __webpack_require__ (bootstrap:79)
    at Module../src/app/app.component.ts (main.js:112)
    at __webpack_require__ (bootstrap:79)
    at Module../src/app/app.module.ts (app.component.ts:12)
    at __webpack_require__ (bootstrap:79)
    at Module../src/main.ts (main.ts:1)
    at __webpack_require__ (bootstrap:79)
    at Object.0 (main.ts:12)

in runtime.

so you can check the error there: https://github.com/furozen/PeerJs-example

@afrokick afrokick added the bug label Nov 1, 2019
@linewei
Copy link

linewei commented Nov 22, 2019

I think it's not a BUG, It works in my angular 8 project.
you should add index.d.ts file to project. you can find this file in root directory.
DO NOT need import peerjs ! just to use Peer class ,because it has been declared in index.d.ts file.

@jloosli
Copy link

jloosli commented Nov 22, 2019

So I've added peerjs (v1.1.0) to an Angular app. Initially, I had no issues or errors. Peerjs wasn't really utilized until you navigated through 2-3 steps and it just worked. I added another part to the app, which fired up peerjs upon landing and that's when I started seeing the parcelRequire error. I added the parcelRequire initialization from #552 (comment) and it seems to just work now. I'm guessing there's a race condition somewhere, but I haven't spent the time to dig into it further.

@adriano-di-giovanni
Copy link

Same problem on Angular 9. I added both esModuleInterop: true and allowSyntheticDefaultImports: true to tsconfig.json with no success. Any news?

@vincent-seibus
Copy link

this works for me as a temp fix in index.html as per @furozen post.

<script>
    var parcelRequire;
  </script>

@jmonster
Copy link

This remains an issue in 2021 when importing peerjs into a lit-element project.
The problem appears to stem from the fact that the built output of peerjs begins with:

parcelRequire=

rather than, you know, declaring the variable

var parcelRequire=

As a result, things just break in my lit-element project, while the browser happily hoists this variable to the global scope and moves on with things.

@jlalmes
Copy link

jlalmes commented Mar 5, 2021

In case anyone is still having trouble - here's how we've implemented a solution.

// shim.js
window.parcelRequire = undefined;
import './shim';
import Peer from 'peerjs';

Make sure you import the shim file before peerjs

@tw-stephen
Copy link

this works for me as a temp fix in index.html as per @furozen post.

<script>
    var parcelRequire;
  </script>

it works thanks for your help

@brianorwhatever
Copy link

<script> var parcelRequire; </script>

this is a sad state of affairs but this seems to work

@afrokick afrokick added this to the 2.0.0 milestone Feb 20, 2022
@afrokick
Copy link
Member

afrokick commented Feb 20, 2022

Could anyone test it in peerjs@beta ? We replace parcel to esbuild and now it should works

jonasgloning added a commit that referenced this issue Apr 27, 2022
- smaller bundle 
- better source maps
- improved bundler compatibility

Closes #845, #859, #552, #585
jonasgloning added a commit that referenced this issue Apr 27, 2022
- smaller bundle
- better source maps
- improved bundler compatibility

Closes #845, #859, #552, #585

.
github-actions bot pushed a commit that referenced this issue May 10, 2022
# [1.4.0](v1.3.2...v1.4.0) (2022-05-10)

### Bug Fixes

* add changelog and npm version to the repo ([d5bd955](d5bd955))
* add token to PeerJSOption type definition ([e7675e1](e7675e1))
* websocket connection string ([82b8c71](82b8c71))

### Features

* upgrade to Parcel@2 ([aae9d1f](aae9d1f)), closes [#845](#845) [#859](#859) [#552](#552) [#585](#585)

### Performance Improvements

* **turn:** lower TURN-latency due to more local servers ([a412ea4](a412ea4))
@nlokhande1
Copy link

nlokhande1 commented Aug 5, 2022

Could anyone test it in peerjs@beta ? We replace parcel to esbuild and now it should works

"peerjs": "^2.0.0-beta.3"
I tried with the import Peer from 'peerjs';
and got :
Type 'typeof Peer' has no construct signatures.

then ,
import DataConnection from 'peerjs';

[!] Error: 'Peer' is not exported by ....\node_modules\peerjs\dist\peerjs.js, imported by src\peerjs.ts

@afrokick

@jonasgloning
Copy link
Member

I tried with the import Peer from 'peerjs';
and got :
Type 'typeof Peer' has no construct signatures.

@nlokhande1 Import syntax for the beta version:

import { Peer } from 'peerjs';

This issue can be closed. Importing from Angular does not require a workaround starting from v1.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests