Edward Liang Edward Liang - 27 days ago 4x
Node.js Question

Why _ as variable name in node REPL mode to require failed

Node version v5.4.1 and I installed lodash in current directory's node_modules. And I encouter this issue where '_' seems special.

> var _ = require('lodash');
> _.defaults
TypeError: Cannot read property 'defaults' of undefined
at repl:1:2
at REPLServer.defaultEval (repl.js:252:27)
at bound (domain.js:287:14)
at REPLServer.runBound [as eval] (domain.js:300:12)
at REPLServer.<anonymous> (repl.js:417:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:210:10)
at REPLServer.Interface._line (readline.js:549:8)
at REPLServer.Interface._ttyWrite (readline.js:826:14)
> var l = require('lodash');
> l.defaults

I saw a question related to undefined output. But no question yet cares about the failure of 'require' when '_' as variable name. Do you know any about it?


The underscore is a special variable that holds the result of the most recent expression. Prior to node v6.0.0, this behavior could not be disabled. However, node v6.0.0 introduced a change that allowed the underscore variable to be overwritten. So you will need to upgrade your copy of node if you want to reassign the _ variable in the node REPL.