net.createServer is not a function error when i tried to add a custom libp2p object to ipfs node

Hey, so I’m trying to get the pubsub feature for my IPFS node in js-ipfs and this is what I’ve written so far:

const IPFS = require("ipfs");

const Libp2p = require("libp2p");

const TCP = require("libp2p-tcp");

const MulticastDNS = require("libp2p-mdns");

const Bootstrap = require("libp2p-bootstrap");

const KadDHT = require("libp2p-kad-dht");

const MPLEX = require("libp2p-mplex");

const { NOISE } = require("libp2p-noise");

const libp2pBundle = (opts) => {

    const peerId = opts.peerId;

    const bootstrapList = opts.config.Bootstrap;

    return new Libp2p({

        peerId,

        addresses: {

            listen: ["/ip4/127.0.0.1/tcp/0"],

        },

        modules: {

            transport: [TCP],

            streamMuxer: [MPLEX],

            connEncryption: [NOISE],

            peerDiscovery: [MulticastDNS, Bootstrap],

            dht: KadDHT,

        },

        config: {

            peerDiscovery: {

                bootstrap: {

                    interval: 30e3,

                    enabled: true,

                    list: bootstrapList,

                },

            },

            pubsub: {

                enabled: true,

            },

        },

    });

};

(async () => {

    console.log("Hello World");

    const ipfs = await IPFS.create({

        repo: String(Math.random() + Date.now()),

        libp2p: libp2pBundle,

    });

    const peers = await ipfs.swarm.peers();

    console.log(

        `The node now has ${

            peers.length

        } peers. and your peerId: ${await ipfs.id()}`

    );

})();

So I used Parcel to bundle everything and i seem to be getting an error saying that net.createServer is not a function. Would really appreciate it if someone could help me out here. Thanks!

Hello @pol905

You are trying to use IPFS in the browser. The TCP transport and the MDNS discovery are not supported in browser environments. You can read more here, but as a short answer you should use websockets + webrtc-star in the browser.

Okay. Thanks for the reply. Gonna try it soon.