What is the node.js analog of python -i: run and enter interactive mode?
I think that the node executable does not allow that you combine the -i
with any other file argument.
This is probably not the solution you would like to read. However, this worked for me. There is module called REPL that basically let you do that manually. So, I realized that I could create a wrapper around any file, as follows:
#!/bin/bash
COMMAND=$(cat <<EOF
(function(){
var repl = require('repl');
process.stdin.push('.load ${1}\n');
repl.start({
useGlobal:true,
ignoreUndefined:true,
prompt:'> '
});
})();
EOF
)
node -e "${COMMAND}"
Supposing you call this script nodejs
, then I can call this script doing something like
nodejs ./demo.js
It starts the REPL programmatically and loads your script into it. This would be equivalent to opening a REPL session manually and then run the command .load <file>
.
Docs at https://nodejs.org/api/cli.html
-r, --require module
Preload the specified module at startup.
Follows require()'s module resolution rules. module may be either a path to a file, or a node module name.
node -i -r ./myprogram.js
-e, --eval "script"
Evaluate the following argument as JavaScript. The modules which are predefined in the REPL can also be used in script.
node -i -e "console.log('A message')"
These features have been added since the previous response in 2014 with the following pull requests.
- https://github.com/nodejs/node/pull/2253
- https://github.com/nodejs/node/pull/5655