Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught ReferenceError: require is not defined #3037

Closed
shivansh-narayan opened this issue Aug 16, 2019 · 8 comments
Closed

Uncaught ReferenceError: require is not defined #3037

shivansh-narayan opened this issue Aug 16, 2019 · 8 comments

Comments

@shivansh-narayan
Copy link

var Web3 = require("Web3");

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

@michaelsbradleyjr
Copy link
Contributor

try:

var Web3 = require('web3');

The package name starts with a lower case w.

@shivansh-narayan
Copy link
Author

@michaelsbradleyjr still the same

@michaelsbradleyjr
Copy link
Contributor

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

@nivida
Copy link
Contributor

nivida commented Aug 18, 2019

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 nivida closed this as completed Aug 18, 2019
@myq
Copy link

myq commented Dec 20, 2019

@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?

@michaelsbradleyjr
Copy link
Contributor

@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.

@alcuadrado
Copy link

@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.

@MukeshJaiswal01
Copy link

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

<title>first dapp</title> <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" integrity_no="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

First Smart Contract

<script type=""> var Web3 = require('web3'); var web3 = new Web3('http://localhost:8545'); var conrtract_abi = [{ "constant": false, "inputs": [ { "internalType": "string", "name": "My_Message", "type": "string" } ], "name": "put", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "getvalue", "outputs": [ { "internalType": "string", "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" } ]; var contact_address = '0x62Fbd0989fbc743dDF11DA5b18e34d9Ffc59a199'; var simple_contract = new Web3.eth.Contract(conrtract_abi, contact_address); console.log(simple_contract) Web3.eth.getAccounts().then(console.log); </script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants