0

In my package.json file, I have:

  "devDependencies": {
    "chai": "^4.1.2",
    "chai-as-promised": "^7.0.0",
    "chai-bignumber": "^2.0.0",
    "decimal.js": "^10.0.0",
    "ethereumjs-testrpc-sc": "6.1.2",
    "ganache-cli": "6.1.0",
    "solidity-coverage": "0.4.14",
    "truffle": "4.1.3"
  }

I created a NodeJS script file getWeb3Version.js:

let web3 = require("web3");
console.log(web3.version);

When I run node getWeb3Version.js before npm install, I get 1.0.0-beta.30.

When I run node getWeb3Version.js after npm install, I get undefined.

In order to investigate deeper, I changed console.log(web3.version) to console.log(web3), and it appears that the web3 object after npm install is a very small subset of the web3 object before npm install.

In order to investigate even deeper, I called npm list --depth=0 from inside and from outside of my project folder (where the package.json file is located).

When I call npm list --depth=0 from inside of my project folder, I get this:

+-- chai@4.1.2
+-- chai-as-promised@7.1.1
+-- chai-bignumber@2.0.2
+-- decimal.js@10.0.0
+-- ethereumjs-testrpc-sc@6.1.2
+-- ganache-cli@6.1.0
+-- solidity-coverage@0.4.14
`-- truffle@4.1.3

As you can see, web3 is not even there (which is obvious, because I do not import it in my package.json file).

Nevertheless, require("web3") seems to work, as mentioned at the beginning of the question.

When I call npm list --depth=0 from outside of my project folder, I get this:

+-- chai@4.1.2
+-- decimal.js@9.0.1
+-- ethereumjs-testrpc-sc@6.0.7
+-- ganache-cli@6.1.0
+-- log4js@2.5.2
+-- pug@2.0.0-rc.4
+-- solc@0.4.19
+-- solidity-coverage@0.4.14
+-- solidity-parser-sc@0.4.4
+-- sprintf-js@1.1.1
+-- testrpc@0.0.1
+-- truffle@4.1.7
`-- web3@1.0.0-beta.30

Followed by a bunch of lines starting with npm ERR! extraneous:.

What exactly is going on here?

Is it possible that one of the packages imported in my package.json file exports the subset of the web3 object that I see when I do console.log(web3)?

halfer
  • 19,824
  • 17
  • 99
  • 186
goodvibration
  • 5,980
  • 4
  • 28
  • 61
  • I would list it as a dependency in my `package.json` to avoid any confusion, global modules are usually installed for clis and stuff like that, in other cases it's better to specify modules in `package.json` – Olga Jun 07 '18 at 05:20
  • npm list will not give you full list. You can check `node_modules` folder, i bet you will have local version of `web3`. – Sunil B N Jun 07 '18 at 05:27
  • @SunilBN: Thank you. I have `node_modules` inside my project folder (but I've explained about that one in my question, `web3` is not there). I have another one at `C:\Users\my_username`, and another one at `C:\Users\my_username\AppData\Roaming\npm`. And as you might guess, I am running on Windows. What exactly am I supposed to do with all of these? – goodvibration Jun 07 '18 at 05:32

2 Answers2

1

is a very small subset of the web3 object is obtained after the npm install

Is because, one of your installed module is using a different version of web3. And when you did not do an npm install, the web3 package is obtained from the global packages which is web3@1.0.0-beta.30.

The key thing to remember here is that that local package overrides the global package.

Akash Dathan
  • 4,348
  • 2
  • 24
  • 45
  • Thank you. However, I have followed the instruction in [this answer](https://stackoverflow.com/a/15597395), and removed all globally installed packages from my system. I admit that I do see yet another place where global packages might be located, at `C:\Users\my_username\node_modules`, so perhaps this answer is incomplete... – goodvibration Jun 07 '18 at 05:30
  • try `npm ls web3`, you will get more clarity. – Akash Dathan Jun 07 '18 at 05:31
  • When I do this outside my project folder, I get `web3@0.18.4 extraneous` under `solidity-coverage@0.4.14` and `web3@1.0.0-beta.30` at the top-level. When I do this inside my project folder, I get `web3@0.18.4` under `solidity-coverage@0.4.14`. – goodvibration Jun 07 '18 at 05:37
  • this command lists both the ones available outside and inside, as you have deleted the global modules, you will only get one, that is the one installed by the `npm install`. If you try to use the module now after deleting your node_modules, it will throw man error now – Akash Dathan Jun 07 '18 at 05:41
0

If the package is not listed in your dependencies and you are still able to use it, it may be because it is installed globally. You can check whether it is installed globally by doing npm ls -g Also unlike chai,ganache-cli, web3 is an application dependency so try to find it on depemdencies instead of dev dependencies

Rubin bhandari
  • 1,873
  • 15
  • 20