Skip to content Skip to sidebar Skip to footer

Prevent Node.js Repl From Printing Output

If the result of some javascript calculation is an array of 10,000 elements, the Node.js repl prints this out. How do I prevent it from doing so? Thanks

Solution 1:

Why don't you just append ; null; to your expression?

As in

new Array(10000); null;

which prints

null

or even shorter, use ;0;


Solution 2:

Assign the result to a variable declared with var. var statements always return undefined.

> new Array(10)
[ , , , , , , , , ,  ]

> var a = new Array(10)
undefined

Solution 3:

Node uses inspect to format the return values. Replace inspect with a function that just returns an empty string and it won't display anything.

require('util').inspect = function () { return '' };


Solution 4:

Javascript has the void operator just for this special case. You can use it with any expression to discard the result.

> void (bigArray = [].concat(...lotsOfSmallArrays))
undefined

Solution 5:

You could start the REPL yourself and change anything that annoys you. For example you could tell it not to print undefined when an expression has no result. Or you could wrap the evaluation of the expressions and stop them from returning results. If you do both of these things at the same time you effectively reduce the REPL to a REL:

node -e '
    const vm = require("vm");
    require("repl").start({
        ignoreUndefined: true,
        eval: function(cmd, ctx, fn, cb) {
            let err = null;
            try {
                vm.runInContext(cmd, ctx, fn);
            } catch (e) {
                err = e;
            }
            cb(err);
        }
    });
'

Post a Comment for "Prevent Node.js Repl From Printing Output"