[Ethereum] nodejs 3.10.9, installation web3, ReferenceError: web3 is not defined

web3js

I'm running:

  • nodejs 3.10.9
  • geth 1.4.18-stable-c72f5459

I did the folowing steps:

  1. run geth --testnet --rpc
  2. installed web3 via npm install web3 -g.
  3. run node (console)
  4. when I am in the node-console and run: console.log(web3);

I get the error:

ReferenceError: web3 is not defined
at repl:1:1
at REPLServer.defaultEval (repl.js:262:27)
at bound (domain.js:287:14)
at REPLServer.runBound [as eval] (domain.js:300:12)
at REPLServer.<anonymous> (repl.js:431:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:211:10)
at REPLServer.Interface._line (readline.js:550:8)
at REPLServer.Interface._ttyWrite (readline.js:827:14)

When I run require('web3') in node console I get the same error.

I also tried to install web3 without -g (npm install web3); same result.

What am I doing wrong?
Why is web3 not recognized by node after the installation through npm install web3 -g?

I tried to do the installation according to this thread:
How to connect NodeJS to the Ethereum network?

and the documentation:
https://www.npmjs.com/package/web3

Best Answer

You need to instantiate it first. after the npm install web3 in node type :

var web3 = require('web3');

then the console.log(web3) will output

{ [Function: Web3]
  providers: { HttpProvider: [Function], IpcProvider: [Function] } }
undefined

for more info read : https://www.npmjs.com/package/web3