First i want to add file to ipfs and retrieve CID, so i did by this:-
const IPFS = require("ipfs");
async function main() {
const node = await IPFS.create();
const version = await node.version();
console.log("Version:", version.version);
const fileAdded = await node.add({
path: "FILE_PATH",
content: "DESCRIPTION",
});
console.log("Added file:", fileAdded.path, fileAdded.cid);
}
main();
then i want create a json file using CID from ipfs and meta information of CID and then upload the JSON to IPFS and retrieve the CID. So i was trying this:-
Read the file into a buffer and use ipfs-add to send the file to ipfs.
We will get the File-CID which we will include when you create the json-object.
Then Read the json-object into a buffer and add that buffer.
It will return the JSON CID.
by this:-
const IPFS = require("ipfs");
async function main() {
const node = await IPFS.create();
const version = await node.version();
console.log("Version:", version.version);
const fileAdded = await node.add({
path: "/home/dungexn/Pictures/1.png",
content: "Image",
});
// console.log("Added file:", fileAdded.path, fileAdded.cid);
// ...
const jsonAdded = await node.add({
cid: fileAdded.cid ,
metadata: {
version: fileAdded.cid.version,
code: fileAdded.cid.code,
},
});
var dictstring = JSON.stringify(fileAdded);
var fs = require('fs');
fs.writeFile("cid.JSON", dictstring, function(err, result) {
if(err) console.log('error', err);
});
var dictstring = JSON.stringify(jsonAdded);
var fs = require('fs');
fs.writeFile("CID.JSON", dictstring, function(err, result) {
if(err) console.log('error', err);
});
const json_data = require('./cid.JSON');
//console.log(json_data)
// fs.readFile(json_data, 'utf8', function (err, data) {
// try {
// // data = JSON.parse(data)
// // for (let i in data){
// // console.log('Name:',data[i])
// // }
// // console.log(data)
// } catch (e) {
// // Catch error in case file doesn't exist or isn't valid JSON
// }
// });
}
main();
the i started getting this error, and i did reinstall ipfs but still:-