blue-sky
blue-sky

Reputation: 53906

Learning node - not finding library

I’m attempting to get node with sample running using library : http://bitcoinjs.org/

After installing I run the code via the node repl (I start repl by running 'node' from terminal):

var keyPair = bitcoin.ECPair.makeRandom()

But receive error :

ReferenceError: bitcoin is not defined
    at repl:1:15
    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)

Do I need to require the bitcoin library before I run in node repl ?

Upvotes: 0

Views: 220

Answers (1)

Cory Forward
Cory Forward

Reputation: 116

Yes, in NodeJS you'll need to import all of your modules as variables before using them. In your instance, you can do this:

var bitcoin = require('bitcoinjs-lib');
...
var keyPair = bitcoin.ECPair.makeRandom()

Edit:: Quick clarification as to what I mean by 'import all of your modules as variables': NodeJS has a simple module loading system. The 'require' statement can import from your global or local packages, or from a relative path. More or less it's telling the Node runtime to look for the JavaScript file with the name provided by the statement. One thing to also note in your REPL, if a module isn't installed as a global module and you're not in a directory that contains the 'bitcoinjs-lib' package as a dependency, you won't be able to require it.

Upvotes: 2

Related Questions