6

I'm trying to create a npm module with functions for interact with the ethereum blockchain, for the interaction i'm using the web3js API and i will put this module inside the my new module.

In my packaje.json i put this:

"dependencies": { "web3": "^0.18.3" }

I'm installing the module with the option --save and in the folder node_module i have all the modules required for web3 and this one installed.

So when i try to test the functionality of my new module i obtain this error:

web3 = new Web3(new Web3.providers.HttpProvider("http://ropsten.infura.io/")
);          ^
ReferenceError: Web3 is not defined

Why is happening this if i have installed the module? Some idea?

More information:

I call my new module with:

 var ether = require('./lib/newmodule.js');

Inside the newmodule.jsi have this for the conection:

if (typeof web3 !== 'undefined') {
    web3 = new Web3(web3.currentProvider);
} else {
    // set the provider you want from Web3.providers
    web3 = new Web3(new Web3.providers.HttpProvider("http://ropsten.infura.io/"));   
    if(!web3.isConnected())
        console.log("not connected");
    else
        console.log("connected");
}

and this for use the module:

module.exports = methods;

And inside methods one methode for print the data and test if work or not the js. In a other Dapp project the newmodule.js works fine, but the web3js is installed by meteor.js so i think's i'm not doing something well with the web3.js installation or building the structure of the module.

5
  • 2
    Did you use var Web3 = require('web3')? Jun 29 '17 at 8:41
  • i need to put it inside the js for use the api?
    – Gawey
    Jun 29 '17 at 9:25
  • Yup. You have installed the library, but you need to import it in order to use it in you js. Jun 29 '17 at 10:04
  • Yes it solved the problem and yes, you have reason.
    – Gawey
    Jun 29 '17 at 14:52
  • 1
    I am adding the answer from the comments so that visitors from future do not have to refine the solution from comments. Jul 3 '17 at 5:40
9

Posting solution from discussions in comments:

The Web3 is not defined because you need to import it where you want to use it, installing web3 alone is not sufficient.

In order to use web3 in you project,follow the steps;

  1. Install web3 using any package manager like npm or bower:
    npm install web3 or bower install web3
  2. Import the web3 liberary in js where you want to use it by using:

    Web3 = require('web3')

You can find details here npm-web3.

2

In my case I replaced web3 with Web3 (capital W) and that worked for me. Just give it a try in your code!

1
  • This worked for me too!
    – YulePale
    Apr 1 at 12:38
2

You can also use the web3 CLI in case you get the error require is not defined.

<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.36/dist/web3.min.js" integrity="sha256-nWBTbvxhJgjslRyuAKJHK+XcZPlCnmIAAMixz6EefVk=" crossorigin="anonymous"></script>

-1
var web3 = new Web3();
   if (window.ethereum) {
    web3 = new Web3(window.ethereum);
    try { 
       window.ethereum.enable().then(function() {
           // User has allowed account access to DApp...
       });
    } catch(e) {
       // User has denied account access to DApp...
    }
 }
 // Legacy DApp Browsers
 else if (window.web3) {
     web3 = new Web3(web3.currentProvider);
 }
 // Non-DApp Browsers
 else {
     alert('You have to install MetaMask !');
 }
  
   window.ethereum.enable()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.