ReferenceError: fetch is not defined {"timestamp":"2024-08-28 18:23:29.135"}

When asking for help in this category, please make sure to provide the following details:

  • SDK Version: 3.2.0

  • Platform: node

  • Browser Console Screenshots:

  • If the issue is related to Custom Authentication, please include the following information (optional):

    • Verifier Name: crowd-wallet-staging
    • JWKS Endpoint:
    • Sample idToken (JWT):

Also, kindly provide the Web3Auth initialization and login code snippet below. This will help us better understand your issue and provide you with the necessary assistance.

const { Web3Auth } = require("@web3auth/node-sdk");
const logger = require("./logger");
const { EthereumPrivateKeyProvider } = require("@web3auth/ethereum-provider");

module.exports = {
  async getWeb3AuthLoginShare(idToken, user) {
    try {
      const privateKeyProvider = new EthereumPrivateKeyProvider({
        config: {
          chainConfig: {
            chainId: "0xaa36a7",
            rpcTarget: "https://rpc.ankr.com/eth_sepolia",
            displayName: "Sepolia",
            blockExplorer: "https://sepolia.etherscan.io/",
            ticker: "ETH",
            tickerName: "Ethereum Sepolia",
          },
        },
      });

      const web3auth = new Web3Auth({
        clientId:
          "BOQxvgUo9bqderAizeiyvGP0gqMuzbk30iYJqGOUjxSuIzuuUmHmMUSp7dKpazj4Fk8KP_rA0ALb8OEprk3YTQw",
        web3AuthNetwork: "sapphire_devnet",
        usePnPKey: false,
      });

      web3auth.init({ provider: privateKeyProvider });

      console.log("before connect");
      const provider = await web3auth.connect({
        verifier: "crowd-wallet-staging",
        verifierId: user.id.toString(),
        idToken,
      });
      console.log("after connect");
      const eth_private_key = await provider.request({
        method: "eth_private_key",
      });
      console.log("ETH PrivateKey: ", eth_private_key);
      const eth_address = await provider.request({ method: "eth_accounts" });
      console.log("ETH Address: ", eth_address[0]);

      return { privKey: eth_private_key, walletAddress: eth_address };
    } catch (err) {
      logger.error(`Error generating Web3Auth login share: ${err}`);
      return null;
    }
  },
};

Thank you for providing the details about the issue you’re facing with the Web3Auth SFA Node SDK. Based on the information provided, here’s a summary that can help our support team understand your issue better:

SDK Details:

  • SDK Name: SFA Node SDK
  • SDK Version: 3.2.0

Error Message:

  • Issue: ReferenceError: fetch is not defined {“timestamp”:“2024-08-28 18:23:29.135”}

Platform:

  • Platform: Node.js

Custom Authentication Details:

  • Verifier Name: crowd-wallet-staging

Browser Console Screenshot:

Web3Auth Initialization and Login Code Snippet:

const { Web3Auth } = require("@web3auth/node-sdk");
const logger = require("./logger");
const { EthereumPrivateKeyProvider } = require("@web3auth/ethereum-provider");

module.exports = {
  async getWeb3AuthLoginShare(idToken, user) {
    try {
      const privateKeyProvider = new EthereumPrivateKeyProvider({
        config: {
          chainConfig: {
            chainId: "0xaa36a7",
            rpcTarget: "https://rpc.ankr.com/eth_sepolia",
            displayName: "Sepolia",
            blockExplorer: "https://sepolia.etherscan.io/",
            ticker: "ETH",
            tickerName: "Ethereum Sepolia",
          },
        },
      });

      const web3auth = new Web3Auth({
        clientId:
          "BOQxvgUo9bqderAizeiyvGP0gqMuzbk30iYJqGOUjxSuIzuuUmHmMUSp7dKpazj4Fk8KP_rA0ALb8OEprk3YTQw",
        web3AuthNetwork: "sapphire_devnet",
        usePnPKey: false,
      });

      web3auth.init({ provider: privateKeyProvider });

      console.log("before connect");
      const provider = await web3auth.connect({
        verifier: "crowd-wallet-staging",
        verifierId: user.id.toString(),
        idToken,
      });
      console.log("after connect");
      const eth_private_key = await provider.request({
        method: "eth_private_key",
      });
      console.log("ETH PrivateKey: ", eth_private_key);
      const eth_address = await provider.request({ method: "eth_accounts" });
      console.log("ETH Address: ", eth_address[0]);

      return { privKey: eth_private_key, walletAddress: eth_address };
    } catch (err) {
      logger.error(`Error generating Web3Auth login share: ${err}`);
      return null;
    }
  },
};

Suggested Solution:

The error ReferenceError: fetch is not defined indicates that fetch is not available in the Node.js environment by default. You can solve this by installing a library like node-fetch.

Try adding the following to your Node.js project:

  1. Install node-fetch:

    npm install node-fetch
    
  2. Require and set fetch globally at the top of your script:

    const fetch = require('node-fetch');
    global.fetch = fetch;
    

This should resolve the fetch is not defined error.


This response was generated by the Web3Auth Bot. A person from the Web3Auth team will also review your issue and provide further assistance.

Used node version 20 and it worked

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.