If you are like me, you should like to test your node modules on the CLI before jumping to your js files.
A couple of days ago I wanted to install and test Underscore as a Node.js module. The install was correct but then I was unable to use it on the CLI. Turns out (sorry for my ignorance by the way) that Node.js’ CLI uses the “_” variable to store information, hence I was not able to get it working untill I decided to use a different name, something like “var __”.
> var _ = require('underscore');
undefined
> _.range(10);
TypeError: Cannot call method 'range' of undefined
at repl:1:4
at REPLServer.eval (repl.js:80:21)
at Interface.<anonymous> (repl.js:182:12)
at Interface.emit (events.js:67:17)
at Interface._onLine (readline.js:162:10)
at Interface._line (readline.js:426:8)
at Interface._ttyWrite (readline.js:603:14)
at ReadStream.<anonymous> (readline.js:82:12)
at ReadStream.emit (events.js:88:20)
at ReadStream._emitKey (tty.js:327:10)
>
The above problem is ONLY for CLI, if you are on a JS file you can use the standard “_” var and everything will work like a charm!
test.js:
var _ = require('underscore');
console.log(_.range(10));
and then on my console:
node test.js [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]