How can I use peer.js on cocos creator 3.7.1

Hello everyone I have a ts file called deneme I am using peer.js in it I add it to the main camera on my project but it gives me error the error message is : [Preview] (intermediate value)(intermediate value)(intermediate value) is not a constructor

My code is like that :

import { _decorator, Component, Node } from 'cc';
import Peer from 'peerjs';
const { ccclass, property } = _decorator;


@ccclass('deneme')
export class deneme extends Component {
    start() {
        const peer = new Peer();
    }

    update(deltaTime: number) {

    }
}

why it gives me the error. How can I use it

Check if it works without the peerjs.
If it does, it could be something wrong with the way you are importing peer.js

Unfortunately no, it did not work my code is like that now :

import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;


@ccclass('deneme')
export class deneme extends Component {
    start() {
        const peer = new Peer();
    }

    update(deltaTime: number) {

    }
}

but it can not find the new Peer();

You should remove all references of Peer, so also the instantiation.

Another way could be to print out Peer in console and see what its true form is

Sorry you were supposed to remove the const peer= new Peer() as well.

when I write the code like that :

import { _decorator, Component, Node } from 'cc';
import Peer from 'peerjs';
const { ccclass, property } = _decorator;

@ccclass('deneme')
export class deneme extends Component {
    start() {
        console.log(Peer);
    }

    update(deltaTime: number) {
        
    }
}

it writes to the console : [Preview] { default: [Getter], Peer: [Getter], util: [Getter] }

then you probably want import { Peer } from 'peerjs'

Thanks but
when I write code like that :

import { _decorator, Component, Node } from 'cc';
import {Peer} from 'peerjs';
const { ccclass, property } = _decorator;

@ccclass('deneme')
export class deneme extends Component {
    start() {
        console.log(Peer);
    }

    update(deltaTime: number) {
        
    }
}

it writes the console : [Preview] undefined

and when I again try that :

import { _decorator, Component, Node } from 'cc';
import {Peer} from 'peerjs';
const { ccclass, property } = _decorator;


@ccclass('deneme')
export class deneme extends Component {
    start() {
        const peer = new Peer();
    }

    update(deltaTime: number) {

    }
}

it gives same error [Preview] (intermediate value)(intermediate value)(intermediate value)
I could not understand it actually ı do not need to add parameters in the constructor I checked the github page of the peerjs in the readme there is an example which is like that that :

const peer = new Peer("pick-an-id");
// You can pick your own id or omit the id if you want to get a random one from the server.

Can you please remove everything that says peer and see if your script is working without it.

import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;


@ccclass('deneme')
export class deneme extends Component {
    start() {

    }

    update(deltaTime: number) {

    }
}

yes it is working without

peerjs seems to have some bug, the constructor is not getting recognized during execution. Not sure how to solve it though :frowning:

thanks for your help actually I tried to change somethings but now I am getting error still but error message has changed the message is now :

Unhandled Promise Rejection: TypeError: undefined is not a constructor (evaluating 'new (_crd && Peer === void 0 ? (_reportPossibleCrUseOfPeer({
              error: Error()
            }), Peer) : Peer)()')

I chose cocos creator for using npm packages like peerjs but it is not very easy I think.

It could be an issue with PeerJs itself. As a last resort, you could include the peerjs library in the html file and in script refer to it using window.Peer. This will be painful because you will not have autocomplete etc but will work.

I was able to get it to create a peer object by using the following

import peerjs from “peerjs”;

for creating the Peer object use this instead.
let peer = new peerjs.Peer()

I do not know why its’ not importing as expected, but this seems to be working give it a shot.
Im’ using CC3.7.2 and “peerjs”: “^1.4.7”

Hope this helps.

thanks you tried to help a lot I wrote my code like you said and also I downloaded cocos creator 3.7.2 to test on it my code is like that :

import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;
import peerjs from "peerjs";
@ccclass('deneme')
export class deneme extends Component {

    start() {
        let peer = peerjs.Peer();
    }

    update(deltaTime: number) {
        
    }
}

but I could not understand it can’t find Peer() gives error Property ‘Peer’ is not type ‘typeof Peer’. ts(2339) why it works on your project but not on mine İ could not understand

im’ assuming you are talking about the error that VSCode is reporting. I get it too, but you can preview the game and see that it gets executed and works fine.

2 Likes

oh yes thanks a lot I did not test it after seeing that error but now I updated my code like that to test if I get any id and yes I got the id

import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;
import peerjs from "peerjs";
@ccclass('deneme')
export class deneme extends Component {

    start() {
        let peer = new peerjs.Peer();
        peer.on("open",function(id){
            console.log("id is : "+id);
        });
    }

    update(deltaTime: number) {
        
    }
}

thank you so much really you saved me