Node.js REPL with lodash
Sometimes we use the Node.js REPL interface to experiment with code. Wouldn’t it be great to have that interface with lodash required by default?
$ npm install -g n_
$ n_n_ >
lodash is now attached to the REPL context as
_, so just use it:
n_ > _.compact;[ 1, 2, 3 ]n_ >
It is possible to use lodash’s functional programming variant
$ n_ --fpn_ > _.map;[ 2, 4, 6 ]n_ >
It is possible to enable strict mode in Node.js >= 4.x:
$ n_ --use_strictn_ >
$ NODE_REPL_MODE=strict n_n_ >
n_ additionally includes all major versions of lodash starting with lodash@^3.10.1 selectable via
n_<major version number>:
$ n_3n_ > _.pluck[Function: pluck]n_ >
If lodash has made another major jump and I didn’t notice, give me a heads-up.
_ character is special in the Node REPL (see nodejs.org/api/repl.html).
In node versions < 6.x n_ redirects this special variable to
$ per default, but you can set your own using the environment variable
SPECIAL_VAR like this:
$ SPECIAL_VAR=my_var n_n_ > 123123n_ > my_var123n_ >
Also note that in node < 6.x using the command
.clear you clear the context lodash is bound to.
The behavior of assigning
_ to the last evaluated expression is disabled, since lodash is already assigned to
n_ stores its session history under