首页 > 解决方案 > IPFS 不是构造函数:Nodejs - IPFS/OrbitDB 聊天室

问题描述

我每次都尝试使用 Nodejs 和 IPFS/OrbitDB 构建 Dapp,但我尝试启动我的应用程序时出现错误:

this.node = 新 IPFS({ ^

TypeError:IPFS 不是构造函数

这是我没有特定 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);

我在以下版本的 IPFS 上运行:ipfs@0.42.0

我也在一个空的 Nodejs 应用程序上尝试了它,当我添加一个特定的 Swarm 来连接时,我也遇到了同样的错误。

非常感谢您的帮助,感谢您提前提供的时间。

亲切的问候

贝尼

标签: javascriptnode.jspublish-subscribeipfsorbitdb

解决方案


我现在这样做了:

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);
        }
    }

(谢谢@尤金)


推荐阅读