Carol.Kar
Carol.Kar

Reputation: 5345

Loading a libary from node-modules into nodeJS

I am running on win 7 and have installed nodeJS as a path variable.

I have installed a nodeJs module(pusher):

When running my script I get:

require('pusher-client')

var API_KEY = 'cb65d0a7a72cd94adf1f'

var pusher = new Pusher(API_KEY);
var channel = pusher.subscribe("ticker.160");
channel.bind("message", function(data) {
    console.log(data);
    var topbuy = data.trade.topbuy;
    console.log("Price: ", topbuy.price,
            "Quantity:", topbuy.quantity,
            "Timestamp:", data.trade.timestamp);
});

Error Message:

$ node pusher.js

c:\xampp\htdocs\projects\psher\node_modules\pusher.js:9
var pusher = new Pusher(API_KEY);
                 ^
ReferenceError: Pusher is not defined
    at Object.<anonymous> (c:\xampp\htdocs\projects\psher\node_mo
dules\pusher.js:9:18)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

I thought with require("insert node_module") the libary is loaded by nodeJS and can be used?

Any recommendations what I am doing wrong?

I appreciate your answer!

Upvotes: 1

Views: 40

Answers (1)

doldt
doldt

Reputation: 4506

You want to save the return value of the require command - that'll be the pusher module's exports. If pusher exports a constructor function, you just want to write:

var Pusher = require('pusher-client');

Then Pusher should be defined and available for use in the way you tried. (btw I copied that line from the linked pusher module readme)

(Also: don't rely on automatic semicolon insertion at the end of your lines, it's bad practice.)

Upvotes: 2

Related Questions