Not initiliazing web3

hello guys there is a problem with my code where it doesnt connect with metamask here is the code :

const Moralis = require('moralis');
const Moralis  = require('moralis/node');

Moralis.serverURL = ''

init = async () => {
    window.web3 = await Moralis.Web3.enable();
    // await window.ethereum.send('eth_requestAccounts');
    // window.web3 = new Web3(window.web3.currentProvider);
    // window.ethereum.enable();

const user = Moralis.User.current();

initUser = async () => {
    if (await Moralis.User.current()){

login = async() => {
    try {
        await Moralis.Web3.authenticate();
    } catch (error) {
        window.alert("Та METAMASK эсвэл түрүүвчээ холбоно уу? ");

hideElement = (element) => = "none"; 
showElement = (element) => = "block  ";

const usrConnectButton = document.getElementById("btnconnect");
const usrPrflButton = document.getElementById("btnusrinfo"); 


Hi bouncyknighter,

The problem is with how Moralis is being imported. Looks like this code is being used in the browser. The easiest way to use the Morlais SDK in the browser is using the CDN links, then you don’t need a require statement at all.

  <script src="[email protected]/dist/web3.min.js"></script>
  <script src=""></script>

Only use a require if you’ve downloaded the moralis npm package. In that case only use the first one for the browser. The node import is only for NodeJS.

const Moralis = require('moralis');

If you put the CDN script tags in your html and delete the require statements from your javascript file it should work. See the docs here:

And the “Dapp in 3 Minutes” tutorial here:

hey, i have removed the required statements, modified the HTML. it is still not initializing the web3 moralis,
here is the HTML.

<!doctype html>
<html class="no-js" lang="mn">

    <script src="[email protected]/dist/web3.min.js"></script>
    <script src=""></script>
  <script src="main.js"></script>
  <meta charset="utf-8">
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <meta property="og:title" content="">
  <meta property="og:type" content="">
  <meta property="og:url" content="">
  <meta property="og:image" content="">

  <link rel="manifest" href="site.webmanifest">
  <link rel="apple-touch-icon" href="icon.png">
  <!-- Place favicon.ico in the root directory -->

  <link rel="stylesheet" href="css/normalize.css">
  <link rel="stylesheet" href="css/main.css">

  <meta name="theme-color" content="#fafafa">

<nav> This is the nav bar lol fuck this shit </nav>
        <button id="btnconnect"> Түрүүвчээ холбох </button> 
        <button id="btnusrinfo"> Таны мэдээлэл </button> 
  <!-- Add your site or application content here -->
  <p>Hello world! This is HTML5 Boilerplate.</p>
  <script src="js/vendor/modernizr-3.11.2.min.js"></script>
  <script src="js/plugins.js"></script>
  <script src="js/main.js"></script>

  <!-- Google Analytics: change UA-XXXXX-Y to be your site's ID. -->
  <script> = function () { ga.q.push(arguments) }; ga.q = []; ga.l = +new Date;
    ga('create', 'UA-XXXXX-Y', 'auto'); ga('set', 'anonymizeIp', true); ga('set', 'transport', 'beacon'); ga('send', 'pageview')
  <script src="" async></script>


Found the problem… move <script src="main.js"></script> from the <head> to the bottom of the <body>. The javascript file is getting executed before the page is rendered.

1 Like

Thanks it did it! The problem was with my boilerplate code, wow it took me almost half a day to refactor the codes re copy paste write it from the start etc. xD