'Undefined' error when using simple DHT commands

I’m trying to run simple DHT commands on nodejs but it returns the following error:
(node:17640) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'findPeer' of undefined

The simple code i’m using is:
const node = await IPFS.create()
const result = await node.dht.findPeer( peerId)

I’m not having problems with other commands such as SWARM PEERS

Thanks for support

Sorry to hear you are having problems - what version of ipfs are you using?

I’m using the 6.14.4. Also I’ve noticed that the ipfs.swarm.peers return very few peers compared to the same command executed with ipfs-http-client

6.14.4 of of what? The latest js-ipfs version is 0.42.0.

Oh sorry, looking with npm I can see that i’m using ipfs v0.42.0 and js-ipfs 0.0.301

The DHT is not enabled in js-ipfs by default, you have to explicitly enable it with:

const node = await IPFS.create({
  libp2p: {
    config: {
      dht: {
        enabled: true
      }
    }
  }
})

Though there’s a regression in the current js-ipfs release that means you can’t enable it, even with the above config (will be fixed by ipfs/js-ipfs#2976.

At any rate the DHT is still considered experimental so will probably cause your node to fall over after a while.

Ok thank you. I tried but as expected it didn’t work. I solved by switching to ipfs-http-client where the same command works