daluege daluege - 1 year ago 45
Node.js Question

Initialize Node.js with script and then provide a console

For testing purposes, I would like to make it possible to spawn a Node.js console that reads commands from

but before runs a bootstrap script to initialize several variables in the local scope.

provide a specific option for this?

I tried to do the trick with the following bash script, but unfortunately, it outputs nothing:

{ cat bootstrap.js; cat; } | node

Answer Source

As it turned out, the script quoted in the question is only missing the node option -i that causes node to

always enter the REPL even if stdin does not appear to be a terminal

As a consequence, this does the trick:

cat bootstrap.js - | node -i

The behavior is still not optimal though as STDIN is printed twice.

See http://stackoverflow.com/a/23037234/449012 for another solution that behaves just like the default console.