How to use Node.js repl with file as input - node.js

I wish to use a js file input as node.js repl, as if it was a command line code:
node -e '<my code>', but with a <filename> instead of an inline code.
How can this be done as the file name as an input, without needing to use another file (such as fs.readFileSync(..))

One way is to use the REPL's .load command and Node's -i flag.
echo ".load test.js" | node -i
Outputs:
$ echo ".load test.js" | node -i
Welcome to Node.js v12.8.1.
Type ".help" for more information.
> .load test.js
2 + 2;
4

Related

Automate Node.js CLI program from shell script

I and trying to execute a node.js CLI program which takes an input from user to decide the next step.
I am able to start the program by
node index.js
after this I want to send 1 as input. Is there a way I can give input to my node.js program.
My dumb brain came up with following script
--Script.sh--
node index.js
1
2
which obviously didn't work.
Maybe this would work?
Create an answers.txt file with each input on its own line:
first
second
then run the command like this:
node index.js < answers.txt
The < will redirect input to the specified file instead of stdin.
What about:
#!/bin/dash
echo "Type something or 'q' to quit and press Enter!"
while read user_input; do
if [ "$user_input" = 'q' ]; then
break
else
echo "You've typed: ""$user_input"
# do something
# node index.js "$user_input"
fi
done

How do I pipe input to a Node.js program that uses readline-sync?

I have a very simple Node.js program that uses readline-sync to accept input, then echo it to the console:
const readlineSync = require('readline-sync');
const input = readlineSync.prompt();
console.log(input);
It works fine as an interactive program; however, when I try to pipe input to it (in either Git Bash or PowerShell), I get a Node.js error:
PS> echo "1.2" | node .\index.js
Windows PowerShell[35552]: c:\ws\src\node_file.cc:1631: Assertion `(argc) == (5)' failed.
Adding a #!/usr/bin/env node shebang and running it as a script with echo "1.2" | .\script.js produces the same error.
Is there a configuration option or something that I'm missing that allows readline-sync to read input from a pipe? Is there something wrong with how I'm running it in the shell? Any advice would be appreciated.
It is most probably the package compatibility issue with the node version that you are using. You need to check all the dependencies whether they are compatible with the node version that you are using.
I think your program is taking 'input' in the form of 'argument' but not from 'stdin'.
when you use '|' , Input will be given to the program as 'stdin' not as 'argument'
so to convert 'stdin' coming out of '|' to 'input argument' we can use 'xargs' in linux.
try following on linux/bash to see if it works :
echo "1.2" | xargs .\script.js
Just to give a example , we can see the functionality of 'echo' command which can take 'input' only in the form of 'arguments' but not as 'stdin' :
# following command does print anything :
echo boo | echo
# but when xargs is used after | , It displays the output :
echo boo | xargs echo
boo

Why pipes are not working on Powershell for various NodeJS CLI tools?

I am trying around the following highly used tools:
prettyjson
prettier
For example when I run the following on Powershell:
echo '{"a": 1}' | prettyjson
The terminal will just keep waiting for inputs till CTRL+C pressed and it exits with no expected output.
The workaround is to add .cmd to the command or just use cmd instead:
echo '{"a": 1}' | prettyjson.cmd
Outputs
a: 1
This seems to be a known limitation and a pull request is available:
https://github.com/npm/cmd-shim/pull/43

Trying to grep instance id from within a text file

I have a text file bak.txt with the following content:
^[[34mINFO^[[0m[0000] Your engine version 1.11.1-cs1 is compatible
^[[34mINFO^[[0m[0000] We detected local components of UCP instance H7LQ:WKR5:G2PX:4F3V:JQ47:WCIG:JV4W:V6SE:4WMR:TLZN:XYWH:MIEQ
^[[31mFATA^[[0m[0000] Re-run the command with "--id H7LQ:WKR5:G2PX:4F3V:JQ47:WCIG:JV4W:V6SE:4WMR:TLZN:XYWH:MIEQ" or --interactive to confirm you want to upgrade this UCP instance.
I am now trying to grep the UCP instance value from bak.txt file using the following command:
grep -Po '(?<=instance=)[^"]*' bak.txt
It is not working. Please suggest the correct way.
Try this grep:
grep -oP '(?<=instance )[^"]+' bak.txt

passing arguments to a node script coming from stdin

overview
I'd like to pass arguments to a node script coming from stdin.
generally, I'm shooting for something like this
nodeScript.js | node {{--attach-args??}} --verbose --dry-run
that would act the same as
node nodeScript.js --verbose --dry-run
more detail
here's a boiled down script for illustration, dumpargs.js
console.log("the arguments you passed in were");
console.log(process.argv);
console.log("");
so you could then:
node dumpargs.js --verbose --dry-run file.txt
[ 'node',
'/home/bill-murray/Documents/dumpargs.js',
'--verbose',
'--dry-run',
'file.js' ]
now the question, if that script comes in across stdin (say, via cat or curl)
cat dumpars.js | node
the arguments you passed in were
[ 'node' ]
is there a good way to pass arguments to it?
not node: with bash, using dumpargs.sh this time
echo "the arguments you passed in were"
printf "> $#"
echo
the answer would look like
cat dumpargs.sh | bash -s - "--verbose --dry-run file.txt"
the arguments you passed in were
> --verbose --dry-run file.txt
There is a specific syntax for this use case. The doc says :
- Alias for stdin, analogous to the use of - in other command line utilities, meaning
that the script will be read from stdin, and the rest of the options are passed to
that script.
-- Indicate the end of node options. Pass the rest of the arguments to the script.
If no script filename or eval/print script is supplied prior to this, then the next
argument will be used as a script filename.
So just do the following :
$ cat script.js | node - args1 args2 ...
For example, this will returns "hello world" :
$ echo "console.log(process.argv[2], process.argv[3])" | node - hello world
This isn't pretty, but works.
The call to node is going to launch the REPL, so your problem should be equivalent to setting / using argv manually from the terminal. Try doing something like:
// argv.js
process.argv[1] = 'asdf';
process.argv[2] = '1234';
and doing cat argv.js dumpargs.js | node.

Resources