i've did the following script which allows me to upload an image on NFT.Storage ipfs server and retrieves the img url.
import { NFTStorage, File } from "nft.storage"
import { mime } from "mime"
import { fs } from "fs"
import { path } from "path"
import { fetch } from "node-fetch"
async function storeNFT(imagePath, name, description) {
const image = await fileFromPath(imagePath)
const NFT_STORAGE_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkaWQ6ZXRocjoweDk5ZmJBOWU3ZTg3RjlENUExZDA3QTJDQTlmNDE4ODNBMGYwNzkyRjgiLCJpc3MiOiJuZnQtc3RvcmFnZSIsImlhdCI6MTY4MDQyNDg5MDI0MiwibmFtZSI6IkF1cm9uQ3JvdyJ9.t7oqb2D9kMYRV1wDJrIiSRNtmyTioqTeNyEjSpfDJvw'
const nftstorage = new NFTStorage({ token: NFT_STORAGE_KEY })
return nftstorage.store({
image,
name,
description,
})
}
async function fileFromPath(filePath) {
const content = await fs.promises.readFile(filePath)
const type = mime.getType(filePath)
return new File([content], path.basename(filePath), { type })
}
async function getImgUrl(imagePath, name, description) {
const result = await storeNFT(imagePath, name, description)
let myUrl = "https://ipfs.io/ipfs/" + result.url.slice(7,80)
console.log(myUrl)
let settings = { method: "Get" };
fetch(myUrl, settings)
.then(res => res.json())
.then((json) => {
console.log(json.image)
let newUrl = "https://ipfs.io/ipfs/" + json.image.slice(7,80)
console.log(newUrl)
})
}
If i run the code using command line, the Upload actually succedes and i get the result i'm looking for, the Image Url, however, i need this to be working on browser side, so i've tried to use browserify and esmify using the following command
browserify index.js -p esmify > bundle.js
But, i'm getting dependencies errors as follows
Can't walk dependency graph: Cannot find module 'ipfs-car/blockstore/fs' from 'node_modules\nft.storage\dist\src\lib.cjs' required by C:\Users\a.marica\Desktop\PersonalWork\Fiverr\Solidity\NFT_Marketplace_Prototype\NFT_Storage_Api\node_modules\nft.storage\dist\src\lib.cjs
I'm not getting why is it able to get all the dependencies and work correctly if launched from command line, but not when launched through browserify in order to convert it to normal js