1

I'm trying to use node.js with my smart contract for my app. As I've tried to convert from using the default app.js set-up to index.js set-up I've ran into this weird issue I can't fix.

It throws the error when trying to instantiate my .json file with truffle-contract.

App.contracts.Flip = TruffleContract(FlipArtifact); //at this line it goes to truffle-contract.js and throws this error: 

var BigNumber = (new Web3()).toBigNumber(0).constructor;

TypeError: Web3 is not a constructor   

I had the exact same thing but in my app.js where truffle puts it automatically and it was working fine.

Could someone please explain the difference between using truffle-contract in my app.js/client-side and my index.js/server-side. Thanks for any help.

0

If you're running in a browser with MetaMask, then Web3 is injected as a side effect of this (although try not to rely on this behaviour). In server-side code, you must import web3 yourself, by putting something like:

const Web3 = require('web3')

near the start of your code. You may also have to npm install web3 or yarn install web3, and if you're using BigNumber (which they no longer bundle with Web3 1.0) then you probably want to specify npm install web3@^0.20.6 or yarn install web3@^0.20.6.

  • Hey @James_pic , I'm currently doing var Web3 = require('web3') and that is working fine because without it said "Web3 not a ...". I will try to npm install web3@0.20.6 and see if that works! – savard Jun 3 '18 at 14:13
1

I had the same error "...Web3 is not a constructor."

The issue was in the order in which I had my following included in the html code:

<script src="./js/truffle-contract.js"></script>
<script src="./js/web3.min.js"></script>

I just made sure web3.min.js script was called before the truffle-contract, and that solved the issue.

Correct order to include:

<script src="./js/web3.min.js"></script>
<script src="./js/truffle-contract.js"></script>
  • Thanks for the answer d5t, for my situation doing what James_pic suggested fixed it. – savard Aug 24 '18 at 16:53

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.