Node.js: Executing a file from the CLI

Using Linux, I can run a Javascript file with node file.js. Is it possible to run file.js after entering the Node CLI using node?

Edit: This is actually possible with the REPL functions, specifically.load. This loads the script into the current terminal, allowing for terminal input for debugging rather than resuming stdin.

The REPL commands can be accessed with .help.

You can't really do this.

What you can do is use require and you can make your file behave differently whether it is running standalone or used by require, which gives you almost what you are looking for.

From the CLI:

mod = require("my_module);
mod.my_function();

To figure if you are running standalone, use a test like this: require.main === module

So file.js becomes:

function my_function() {
  ...
}

exports.my_function = my_function;

if (require.main === module) {
  // I'm running standalone
  my_function();
}