简体   繁体   中英

Running node from a bash script: output console.log

I'm running a node script from bash. Something like:

#!/bin/bash
echo "Executing mynodescript.js..."
node mynodescript.js

Inside "mynodescript.js" I have a console.log("Hello from mynodescript.js"). How can I do to output it on my terminal window? So executing the bash script above would output something like:

> Executing mynodescript.js...
> Hello from mynodescript.js

How about this

#!/bin/bash
echo "Executing mynodescript.js..."
node mynodescript.js 2>&1

The following code in your node script should work: console.log('some text');

If it doesn't, make sure your PATH is correct within the shell script when executing node. The path to your node binary isn't set at the system level (within /etc/paths) then you may need to use the /absolute/path/to/node nodescript.js within the shell script.

If that isn't the issue, verify your node script syntax is correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM