1

I installed it and am trying to use the w3 module (on windows).

I have altered my global repo location to: C:\Users\<user>\.node_modules_global and installed the web3 module with the command bellow, which created a new folder on the node_modules_global folder:

npm install ethereum/web3.js --global

When I try to run:

Web3 = require('web3') it, I get an

Error: Cannot find module 'web3'

however, if I use the fullpath:

Web3 = require('C:\\Users\\<user>\\.node_modules_global\\node_modules\\web3')

It works.

Any idea what could be causing this issue? (I have added C:\\Users\\<user>\\.node_modules_global to the SYSTEM PATH).

TylerH
  • 20,799
  • 66
  • 75
  • 101
Diego
  • 34,802
  • 21
  • 91
  • 134

1 Answers1

3

Your installing it globally, so it is saved to your user folder, rather than in the project, and node is configured by default to look in node_modules.

Two options to fix this:

  • (a) Save Packages Locally Instead
    • Use just npm install ethereum/web3.js or npm install ethereum/web3.js --save to install it to the node_modules directory within your project. (You must have cd'd into your project folder first!)
  • (b) Make Node look in your global folders by default
    • use "NODE_PATH": "C:\\Users\\<user>\\.node_modules_global\\node_modules"

How to install locally, and how to install globally

  • To locally install a module, just do npm install my-module, or if you'd also like to add it to your package.json, then do npm install my-module --save
  • To Globally install a module, use npm install my-module --global

When to use local and global modules

You should:

  • Install a module locally if you're going to require() it.
  • Install a module globally if you're going to run it on the command line.

Source: https://nodejs.org/en/blog/npm/npm-1-0-global-vs-local-installation/

Changing Node path

You can set the NODE_PATH environment variable to your own value, and your application will by default look there, instead of the projects node_modules directory.

See here: http://nodejs.org/api/modules.html#modules_loading_from_the_global_folders

Alicia Sykes
  • 5,997
  • 7
  • 36
  • 64
  • 1
    interesting - I was tryin to avoid local install if I knew I'd be using the same package more than once. What you said seems to have worked, Im just getting a message: npm WARN saveError ENOENT: no such file or directory, open 'C:\git\Crypto\samples\eth1\package.json' should I worry about that? thanks – Diego Nov 07 '17 at 17:10
  • That warning can be fixed by creating a package.json, try ruinning `npm init` and it will make one for you :) It's just a warning, not an error so not too much of a big deal. `package.json` is used to keep track of all your dependencies, see here: https://docs.npmjs.com/files/package.json – Alicia Sykes Nov 07 '17 at 17:13