0

I want to set web3 to Metamask. As mentioned in Metamask document and npmjs, i took a use @metamask/detect-provider. My problem is when i approach window.ethereum. I have a UI, JS file, and some contracts. My app.js code is below. Whenever my http request is /send-to-ipfs i approach this message in console:

@metamask/detect-provider: Unable to detect window.ethereum.
Please install MetaMask!

Please check my app.js code and comment about am i coding correct to set Metamask as web3 provider or not, and in that case guide me to correct code.

app.js:

const fs = require("fs");
const ipfsClient = require('ipfs-http-client');
const Hash = require('ipfs-only-hash')
const contract=require("truffle-contract");
const Web3 = require("web3");
const Window = require('window');
const window = new Window();
const detectEthereumProvider=require('@metamask/detect-provider');
const solc=require("solc");
const express=require('express');
const app=express();
const bodyParser=require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
app.use(express.static('E:/Test'));
app.set('view engine',"pug");
app.set('views','./views');


app.post('/send-to-ipfs', async function(q,r){
    const node= ipfsClient({ host: 'localhost', port: '5001', protocol: 'http' });
    const data=q.body.KeyPad;
    let result=await Hash.of(data)
    node.add(data);
    const provider=await detectEthereumProvider();
    if (provider) {
        startApp(provider); // initialize your app
    } else {
          console.log('Please install MetaMask!');
    }
    r.render('IPFSconnection2.jade',{key: data, cid:result});
});
... here is some code that are not my peoblem ...

2 Answers 2

1

I believe the problem here is that you are trying to retrieve the Metamask provider from the server side, whereas Metamask is a browser extension and therefore, you should do so from the client side.

0

Update your metamask extension. it resolves my same issue

1
  • I did but with no results.
    – Alireza
    Dec 9, 2020 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.