HeyLGT
HeyLGT

Reputation: 33

Disable this message - Expression assignment to _ now disabled? NodeJs-REPL

when I use var _ = require('underscore'), I got this message Expression assignment to _ now disabled..

Is there any way I can use to avoid this message?

I can do change the variable name, but I found someone with the same node and the message did not be raised.

root@other:/# node
> var _ = require('underscore');
undefined
>

root@my:/# node
> var _ = require('underscore');
Expression assignment to _ now disabled.
undefined
>

Upvotes: 1

Views: 1218

Answers (1)

Paul
Paul

Reputation: 36339

So, you can actually define your own custom repl if you want, the docs are here: https://nodejs.org/api/repl.html

For example, if you wanted to change the behavior you're describing you could overwrite the writer function to skip that output or just (probably easier) redefine the context variable itself:

  const repl = require('repl');
  const underscore = require('underscore');

  const r = repl.start('> ');
  Object.defineProperty(r.context, '_', {
     configurable: false,
     enumerable: true,
     value: underscore
   });

Or if you just want to allow it without the error, just do what they did but skip the error message:

  Object.defineProperty(context, '_', {
      configurable: true,
      get: () => this.last,
      set: (value) => {
           this.last = value;
       }
   });

To actually use the above, you need to run the script containing it (as described in the linked docs). This can be done simply with

  node myrepl.js

Or if you re running Linux or MacOS you can make it an executable script and put it in your PATH.

Upvotes: 1

Related Questions