1

After installing bitcoinjs and firing up node and running :

var bitcoin = require('bitcoinjs-lib');

I receive error :

Error: Cannot find module 'bitcoinjs-lib'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    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)

When I run these commands it appears to find the library :

$ pwd
/usr/local/lib/node_modules
$ ls
bitcoinjs-lib   browserify  npm     uglify-js
$ node
> var bitcoin = require('bitcoinjs-lib');
undefined

I think it works in this case because I'm running node from library /usr/local/lib/node_modules

How can I add the modules library to node so can find a required library from any location instead of having to change directory into /usr/local/lib/node_modules?

I've tried modifying path in bash_profile to :

PATH=$PATH:/usr/local/lib/node_modules

But this does not fix issue.

2 Answers 2

3

To get this working the quick way, you need to set $NODE_PATH:

NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules

However, in general, Node modules shouldn't be installed globally (which means in a "system directory" like /usr/local/lib/node_modules).

Instead, you create a project directory, and install the module there:

$ mkdir my-project
$ cd my-project
$ npm install bitcoinjs-lib

If you run Node in that project directory, require('bitcoinjs-lib') will work.

1
  • thanks, I installed globally so can use library from repl
    – blue-sky
    Jun 25, 2016 at 19:55
3

A hacky way:

module.paths.unshift(<path>);
process.env.NODE_PATH = <path>
module.constructor._initPaths();

I figure out the above solution when I wrote a Cocos Creator(based on Electron) plugin which is loaded during the IDE opening. And I need to require a utils script from that plugin which is not located in any require paths. I tested the first code snippet in CC and Node.js v10.16.0 for Win10.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.