Do I need to run an IPFS node to use ipfs.dag.put?

I’m trying to build an application which can upload data to IPFS, based on this tutorial:

I wrote a function like this:

async function createPost(inputData) {  
        const CID = await ipfs.dag.put({
            content: inputData
        return CID;

I’m getting the error:

TypeError: Cannot read property ‘put’ of undefined

I included ipfs like this: const ipfs = require('ipfs');

Should I start an IPFS node before I try to add files?
I don’t understand how to add files.

You need to first start a node:

const node = await ipfs.create()

then elsewhere you can use it to add data:

const cid = await node.dag.put({
  content: inputData

and shut it down when you are done:

await node.stop()
1 Like