this works on windows, mac, and linux.
I'll use the windows version since it's still the most common desktop, but they are all identical expect for the look of the console prompt (as far as using node.js is concerned) in terms of the couple usages shown below.
Once you install node, which is actually really easy these days (next,next,next...), you can use "node" at the command line.
usually this just jumps into an interactive node session, but 2 flags can stop that: -p and -e.
-p prints an evaluated program
-e evaluates a program that follows, so it must be the last flag
very very simple examples:
first, as a pipe destination, same as MORE:
note that if you're piping into node, you don't need the -e flag, it's implied.
C:\>echo 123*456 | node -p
you can interpolate shell expansions into node code:
C:\>echo '%TIME%'.bold() | node -p
second, you can send it a program to the right of the command using -e:
if you want to interpolate in this mode, you need to use a quote around the whole program:
C:\>node -p -e Math.random()
C:\>node -p -e " '%TIME%' + Date() "
2:39:24.44Fri Jan 31 2014 02:39:24 GMT-0600 (Central Standard Time)
BONUS, windows batch file.
this little guy lets you easily execute JS code in a batch file or right in the console.
if nothing else, it's a handy way to get a calculator on the console.
once node is installed, NPM adds itself to the path (reboot may be needed)
so, by adding a simple batch file to that folder, we can create a new shell command that runs in any folder.
the file, js.bat:
setup and demo (type what's in red, [ENTER] after each line):
note: "^Z" means [CTRL]+[Z] in the screen shot below:
C:\Users\dan\AppData\Roaming\npm>copy con js.bat
@node -p -e %1 ^Z
1 file(s) copied.
so, the next time you need a special format date stamp, a random number, or to convert data primitives on the console, node.js is your man!