4

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 
         }
       })   
}***
Sneha
  • 41
  • 1
  • 2

4 Answers4

3

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

npm uninstall --save ipfs-http-client
npm i --save ipfs-http-client@33.1.1

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

vimuth
  • 5,064
  • 33
  • 79
  • 116
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 08 '22 at 16:20
3

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 });

GeoffreyMahugu
  • 381
  • 1
  • 8
  • 13
1

Maybe you're missing the babel loaders I got this working using the following:

npm i ipfs-http-client@50.1.2 @babel/core --save
Ritzy Dev
  • 387
  • 3
  • 10
0

If you take a look at the package at the npm website, you'll see that the newer versions import the create-function as such:

import { create } from 'ipfs-http-client'

Using this, you can simply change your code to

import React, { Component } from 'react'; 
import logo from '../logo.png'; 
import './App.css';
import { create } from 'ipfs-http-client';

const projectId = '*****';
const projectSecret = '***';
const auth =
    'Basic ' + Buffer.from(projectId + ':' + projectSecret).toString('base64');
const ipfs = create({
  host: 'ipfs.infura.io',
  port: 5001,
  protocol: 'https',
  headers: {
    authorization: auth,
  },
});
class App extends Component {
...
}

Henrik Klev
  • 322
  • 1
  • 11
  • 2
    Yeah I tried that. Still getting the same error. – Sneha Jul 01 '22 at 03:27
  • Try purging the node-modules; delete `node-modules` and `package-lock.json`, then run `npm install`. Also, check which version of `ipfs-http-client` you've added to your `package.json`; it should ideally be major version 57. – Henrik Klev Jul 01 '22 at 05:54
  • Yeah, tried that too. Still getting the same error. – Sneha Jul 02 '22 at 17:55
  • What specific version of `ipfs-http-client` are you running? And what version of node and npm are you using? Are you using any IDEA that might interfere, or are you running straight from a terminal? – Henrik Klev Jul 06 '22 at 08:08
  • 2
    were you able to resolve the error? i am facing the same problem. – coder_newbie Aug 01 '22 at 11:15
  • @HenrikKlev i am also facing same problem. my ```ipfs-http-client``` version is ```^57.0.3``` – coder_newbie Aug 01 '22 at 11:18
  • No luck here either. Some other posts says this is a server side module only. I'm not sure why. Is this the reason ? https://github.com/facebook/jest/issues/13549 – jdbertron Jan 05 '23 at 00:11