Web3.js: Uncaught ReferenceError: require is not defined

Created on 16 Aug 2019  路  8Comments  路  Source: ChainSafe/web3.js

var Web3 = require("Web3");

I have downloaded the web3 modules using npm but the above statement in my HTML code is still showing error

All 8 comments

try:

var Web3 = require('web3');

The package name starts with a lower case w.

@michaelsbradleyjr still the same

@narayanshivansh49 are you using some front-end build tooling like webpack? Do you have a repository I can look at?

Using of a bundler is required if you install it from NPM. The minified file can be found here on GitHub in the dist folder.

@nivida The minified file in the dist folder still contains the call to require(), so it can't be used in a browser (as of version 1.2.4).

What is the best way for me to generate a minified version of 1.2.4 that works?

@myq I tried uploading web3.min.js from the dist/ folder on the 1.x branch to codesandbox and then making use of it in a sandbox with:

<script src="https://uploads.codesandbox.io/uploads/user/68052bd8-43d7-466a-af1e-bb0ab2bd3a40/F6-W-web3.min.js"></script>

It seems to work fine, i.e. in the browser console I can see there is indeed a global variable named Web3 and web3 = new Web3() works.

@cgewecke fixed this in #3261, so cloning the repo and building the minified version should work.

I'm not sure if the minified file in dist/ should be updated before a new release though. Should it track the latest released version? I normally don't push autogenerated files, so I'm not sure what's common in this case.

sir , i'm getting same problem
this is my code -->






first dapp

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">






First Smart Contract






Was this page helpful?
0 / 5 - 0 ratings