Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module not found: Can't resolve 'ipfs-http-client' in 'D:\Pro\src\components'

I am trying to send files to ipfs using a website in node-js. I am using the ipfs-http-client module. When i try to access the module using require I keep getting this error :

  1. Module not found: Can't resolve 'ipfs-http-client' in 'D:\Pro\src\components' in the command prompt.

This the error message in the website :

  1. Failed to compile ./src/components/App.js Module not found: Can't resolve 'ipfs-http-client' in 'D:\Pro\src\components' This error occurred during the build time and cannot be dismissed.

I installed the module using the command specified in the official docs - "npm install --save ipfs-http-client" . I can see the module in my dependencies but still getting this error. I am a complete newbie to all this. A little help would be much appreciated. Thanks in advance.

This is how I am accessing the module :

***import React, { Component } from 'react'; 
import logo from '../logo.png'; 
import './App.css'; 

const ipfsClient = require('ipfs-http-client'); 
const projectId = '*****'; 
const projectSecret = '***'; 
const auth =
    'Basic ' + Buffer.from(projectId + ':' + projectSecret).toString('base64'); 
const ipfs = ipfsClient.create({
    host: 'ipfs.infura.io',
    port: 5001,
    protocol: 'https',
    headers: {
        authorization: auth,
    }, 
}); 
class App extends Component {   
    constructor(props) {    
      super(props);
      this.state={
        buffer: null
      };   
    }   
captureFile=(event) => {
    event.preventDefault()
    const file = event.target.files[0]
    const reader = new window.FileReader() 
    reader.readAsArrayBuffer(file)
    reader.onloadend=() => {
      this.setState({buffer: Buffer(reader.result) }) 
    }
    console.log(event.target.files)   
}   
onSubmit = (event) => {
    event.preventDefault()
    console.log("Submitting the form...")
       ipfs.add(this.state.buffer, (error,result) => {
         console.log('Ipfs result', result)
         if(error){
           console.error(error)
           return 
         }
       })   
}***
like image 430
Sneha Avatar asked Oct 29 '25 09:10

Sneha


2 Answers

Try using earlier version I have just tried it. Do the following :

npm uninstall --save ipfs-http-client
npm i --save [email protected]

I do not know what the problem is with the updated version but this is a quick fix up for now. And will get your code running

like image 76
Zain ul Abidin Avatar answered Oct 31 '25 00:10

Zain ul Abidin


Import it like this:

const ipfsClient = require('ipfs-http-client');

Then create the connection:

const ipfs = ipfsClient.create(https://ipfs.infura.io:5001);

To upload:

const uploadFile = await ipfs.add({ content: file });

like image 34
GeoffreyMahugu Avatar answered Oct 31 '25 00:10

GeoffreyMahugu