In Nodejs REPL env, where var _ = require('lodash') failed to be assigned, but abc can.
> var _ = require('lodash');
undefined
> _
undefined
> var abc = require('lodash');
undefined
> abc
{ [Function: lodash]
templateSettings:
{ escape: /<%-([\s\S]+?)%>/g,
evaluate: /<%([\s\S]+?)%>/g,
interpolate: /<%=([\s\S]+?)%>/g,
variable: '',
imports: { _: [Circular] } },
In the repl documentation it mentions that the _ variable is special and always refers to the result of the last expression. In other words, you cannot use _ in the repl for your own use, as it will get overwritten every time you execute a "line" of code.
the use of _ is so common for lodash or underscore. I don't have any reason why _ is chosen to be the default result. . would be far more acceptable than _
. is not a valid identifier
Just out of interest I thought I've do some provenance work on this
_ was introduced in repl.js back in the 25th of September 2009 @ https://github.com/nodejs/node/commit/7674bd500418cba020c69d0f5702ad45ec61d2a9
Underscore was born almost exactly one month later, on the 26th of October 2009 @ https://github.com/jashkenas/underscore/commit/02ede85b539a89a44a71ce098f09a9553a3a6890
Perhaps we should go argue to Underscore and lodash that they should change to something that won't conflict with Node?
FWIW Chrome console uses $_ for this and it works the same way
To add to what @rvagg said, the underscore's behavior was copied from the python and ruby REPLs.
I think __ or __last or __xxx would be much acceptable where __ is known to be system reserved.
It's been _ for over half a decade now, it's not going to change.
We could try two things:
_;_ is defined;@vkurchatkin
I would say that is a very good idea.
:100:
I like that suggestion @vkurchatkin, would lead to fewer surprises like the one that prompted this issue
Why not just disable _ functionality with a warning once it's being assigned to? Not being able to paste lodash examples in the REPL is not ideal, imho.
I suggest to add an entry in help command like below
node 🙈 ₹ node
> .help
break Sometimes you get stuck, this gets you out
clear Alias for .break
exit Exit the repl
help Show repl options
load Load JS from a file into the REPL session
save Save all evaluated commands in this REPL session to a file
_ Bound to last successfully evaluated expression
>
see #5438 for one option
Most helpful comment
. is not a valid identifier