4 ms·
Small thing but I dig the new --eval flag. To do something like this in node, I used to do: (echo 2+2; cat -) | node -i Which would generate some stdin via
by rektide 5y ago
Small thing but I dig the new --eval flag.
To do something like this in node, I used to do:
(echo 2+2; cat -) | node -i
Which would generate some stdin via echo, then use cat to feed stdin to node. --eval 2+2 is a blissfully elegant upgrade to this.
As a comment points out, node has a '-e' (and a -p which prints the evalled thing) but does not open a repl after. This shell receipt, like --eval, leaves the repl open.
- deleted 5y ago[deleted]
- johannes1234321 5y ago1) node has a -e option 2) why the cat? It just pipes the stuff through 3) you can use <<< to pipe something to stdin (`node <<< 1+1`)
- codetrotter 5y agoThey wanted to keep feeding stdin that’s why. Their 2+2 is just a placeholder for something bigger, which could even be several lines of input that they wanted to prepend, followed by manual input. In other words they are talking about initiating a REPL session with some initial input followed by manual input.
- dsherret 5y agoJust to be clear, `deno eval "console.log(5)"` has existed for some time which is similar to `node -e "console.log(5)"`. In this case, the deno repl subcommand's --eval flag provides a cross-platform way to evaluate some setup code before launching into the REPL. This is useful if you alias the command in your shell's initialization file allowing you to easily launch a REPL with everything already setup to your liking. For example: $ alias add_repl='deno repl --eval "const add = (a, b) => a + b"' ... $ add_repl Deno 1.13 exit using ctrl+d or close() > add(1, 2) 3
- lxe 5y agoNode's `-p` option will print the result of the evaluation. $ node -p 1+1 2
- dsherret 5y agoThe equivalent in Deno is: $ deno eval -p "1 + 1" 2