Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
Exported functions are not defined. Parcel/Browserify

When I use a bundler to make my JS browser compatible my exported functions are not exported.

Error: (index):11 Uncaught TypeError: connect is not a function at HTMLButtonElement.onclick ((index):11:46)

index.js (Same root file as index.html)

const {ethers} = require("ethers");

async function connect() {
    if (typeof window.ethereum !== 'undefined') {
        await ethereum.request({method: "eth_requestAccounts"});
      }
  }

  module.exports = {
    connect
  }

index.html (Same root file as index.js)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>HTML 5 Boilerplate</title>
    <script src="./index.js" type="module"></script>  
  </head>
  <body>
    <button id="connect" onclick="connect()">   Connect  </button>

  </body>

</html>

Using parcel, which auto creates a dist file and starts a local server. Also tried browserify and had same issue.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Parcel supports script tag/elements of type module.

This code works for me but window.ethereum is undefined, it's related to MetaMask no?

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>HTML 5 Boilerplate</title>
  </head>
  <body>
    <button id="connect">Connect</button>

    <script type="module">
      //Import your module
      import something from "./index.js";

      //Assign event listener to button
      document
        .getElementById("connect")
        .addEventListener("click", function (event) {
          something.connect();
        });
      console.log("something", something);
      /* JavaScript module code here */
    </script>
  </body>
</html>

index.js

const { ethers } = require("ethers");

async function connect() {
  console.log("Inside connect function");
  console.log("window.etherum", window.ethereum);
  console.log("ethers", ethers);
  if (typeof window.ethereum !== "undefined") {
    await ethereum.request({ method: "eth_requestAccounts" });
  }
}

module.exports = {
  connect,
};
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda