1

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
>
HeyLGT
  • 33
  • 5
  • 1
    Use a different variable name? – robertklep Sep 09 '18 at 09:28
  • I can do that, but I find someone with the same node and the message could not be raised. So I want to figure out how to do that. – HeyLGT Sep 09 '18 at 09:34
  • 1
    AFAIK, you can't disable the warning. It's there for a reason (see [this issue and its comments](https://github.com/nodejs/node/issues/5431)). And the process's environment is unrelated. – robertklep Sep 09 '18 at 09:40
  • But there is someone who have disabled this warning. – HeyLGT Sep 09 '18 at 09:45
  • And they are also running Node 8.11.4? Ask them how they did it. – robertklep Sep 09 '18 at 09:46
  • Yes, it is the same node version. they did not answer my email – HeyLGT Sep 09 '18 at 09:54
  • Nothing [in the code that implements the REPL](https://github.com/nodejs/node/blob/v8.11.4/lib/repl.js#L653-L663) suggests that it can be disabled or turned off, not even for v8.11.4, so I have my doubts that they managed to actually disable it. – robertklep Sep 09 '18 at 09:59
  • Yes, I have read the same source code. So I think the only way to remove this message it that redirect the output stream or do some black magic. So I carefully compare our env. I only find this - `_` is the only difference. They and I both use the docker to virtualize the base node env, therefore there are few difference between us. – HeyLGT Sep 09 '18 at 10:07

1 Answers1

1

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.

Paul
  • 35,689
  • 11
  • 93
  • 122
  • Can I ask you if I want change the repl.js source code, where is the repl.js file? – HeyLGT Sep 10 '18 at 03:15
  • How should I do to define the context in global? Because I can do that once, then when I use the command `node` on every occasion, I do not need to do that again. – HeyLGT Sep 10 '18 at 03:25
  • Don’t change the repl.js source code. Define it as a custom command and just use that instead. I’ve edited my answer to reflect. – Paul Sep 10 '18 at 11:33
  • I mean if I want to just use command `node` without the specific `repl.js` to achieve that, is there some way I can define the global context? Just like the situation I mention above in the question about other machine. – HeyLGT Sep 11 '18 at 01:55