javascriptnode.jspublish-subscribeipfsorbitdb

IPFS is not a constructor: Nodejs - IPFS/OrbitDB chatroom


I'm trying to build a Dapp with Nodejs and IPFS/OrbitDB every time, I try to start my App I get the error:

this.node = new IPFS({ ^

TypeError: IPFS is not a constructor

This is my basic code without a specific Swarm:

const Ipfs = require('ipfs');
const OrbitDB = require('orbit-db');

class chatroom {
    constructor(IPFS, OrbitDB) {
        this.OrbitDB = OrbitDB;
        this.node = new IPFS({
            preload: {enable: false},
            repo: "./ipfs",
            EXPERIMENTAL: {pubsub: true},
            config: {
                Bootstrap: [],
                Addresses: {Swarm: []}
            }
        });
        this.node.on("error", (e) => {throw (e)});
        this.node.on("ready", this._init.bind(this));
    }
    async _init(){
        this.orbitdb = await this.OrbitDB.createInstance(this.node);
        this.onready();
    }
}

module.exports = exports = new chatroom(Ipfs, OrbitDB);

I'm running on the following version of IPFS: ipfs@0.42.0

I tried it also on an empty Nodejs App and there I had the same error also when I added a specific Swarm to connect to.

I would really appreciate your help, thx for your time in advance.

Kind regards

beni


Solution

  • I did it now like that:

    const IPFS = require('ipfs');
    
    async function createNode() {
            let node = await IPFS.create(
                {
                    repo: (() => `repo-${Math.random()}`)(),
                        "Addresses": {
                            "Swarm": [
                                "/ip4/0.0.0.0/tcp/4001"
                            ],
                            "API": "/ip4/127.0.0.1/tcp/5001",
                            "Gateway": "/ip4/127.0.0.1/tcp/8080"
                        }
                }
                );
            try {
                await node.start();
                console.log('Node started!');
            } catch (error) {
                console.error('Node failed to start!', error);
            }
        }
    

    (thx @Eugene)