Node: assigning _ in the repl has side effects

Created on 25 Feb 2016  Â·  14Comments  Â·  Source: nodejs/node

  • Version: v4.2.3
  • Platform: Linux eric-ThinkPad-X220 3.19.0-51-generic #57~14.04.1-Ubuntu SMP Fri Feb 19 14:36:55 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

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] } },
repl

Most helpful comment

. is not a valid identifier

All 14 comments

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:

  • warn on assignment to global _;
  • disable this behaviour when global _ 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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

filipesilvaa picture filipesilvaa  Â·  3Comments

Icemic picture Icemic  Â·  3Comments

addaleax picture addaleax  Â·  3Comments

danielstaleiny picture danielstaleiny  Â·  3Comments

loretoparisi picture loretoparisi  Â·  3Comments