'Copying and pasting code into the Nodejs REPL using shell script
I want to copy a javascript code snippet into Nodejs REPL using shell script. For example
sum.sh
node // Here REPL open, I want copy below codes to this REPL and run
var num1 = 1
var num2 = 2
var sum = num1 + num2
sum
After run sum.sh, expecting result
3
Can we have a solution?
Solution 1:[1]
JavaScript does not run in the terminal. To run JavaScript in a Node.js file, you would install Node.js, create the script, and then run it from the terminal.
First, create the Node.js file which we'll call sum.js:
let num1 = 1;
let num2 = 2;
let sum = num1 + num2;
console.log(sum);
To run this file from the terminal we would type node sum.js
.
To run this from a script, simply create a script, sum.sh
with executable permissions to have the following:
node sum.js
It's the same thing as running it from the terminal, but now it's in a script file.
Solution 2:[2]
You can't directly copy paste command into NodeJS REPL. I think you should try this one.
run command node
and then running on REPL cli as follows .load sum.sh
.
This would be run line by line as expected.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | JSBach |
Solution 2 | Yugo Gautomo |