javascriptnpmsoliditytrufflebignumber.js

require('bignumber.js') fails in truffle test, npm ls -g shows it's installed (Windows)


I'm trying to test solidity code with truffle. As the number values returned from (or sent to) contracts are BigNumbers I want to require the library. My current test is a one-liner:

let BigNumber = require('bignumber.js');

called hello_test.js.

If I run npm ls -g | grep bignumber I can see +-- bignumber.js@7.2.1, so it's installed; but if I try:

truffle.cmd test .\test\hello_test.js

I get

Error: Cannot find module 'bignumber.js'

What's going on?

Truffle version 4.0.6, npm version 5.6.0


Solution

  • This is the way node require works: require('xx') will search for

    ./node_modules/xx.js
    ./node_modules/xx/index.js
    ./node_modules/xx/package.json
    

    If it can not find, it will go ahead to search by the environmental variable, NODE_PATH. If that is not specified, it won't search any global module.

    So you can check your NODE_PATH.