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.

3 Answers 3


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 add 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 [email protected] and see if that works!
    – savard
    Jun 3, 2018 at 14:13
  • 1
    FYI for later readers: auto-injection is outdated.
    – WBT
    Nov 14, 2019 at 21:58
  • this works for me in node v16, but get "not a constructor" after upgrading to node v18 ... TBH, I've had nothing but problems getting anything in web3 working. No example code, nothing works, no documentation - worst experience ever Jun 8, 2023 at 14:13

I was stuck with the same error while trying to use this in nodejs..

I finally found the fix. Apparently, in Web3.js 4.x, we're supposed to import it like this:

const { Web3 } = require('web3');

Instead of this

const Web3 = require('web3');

Refer Docs

  • 2
    Sheesh, I'm new to all this, but had working scripts as of last May. Crazy how by September those scripts are already dated for any new web3 install and needed this { } fix. Thanks yo. This was the solution.
    – VanGogh66
    Sep 17, 2023 at 18:21
  • This should be the accepted answer. Nov 12, 2023 at 21:18
  • Saved me ton of time. With node >= 18 this is only acceptable answer
    – DruidKuma
    Mar 9 at 19:39

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, 2018 at 16:53
  • 2021 and this is what worked for me. Thanks! May 10, 2021 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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