1

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?

2
  • I don't believe you can... not directly anyway. Commented Feb 25, 2013 at 22:21
  • It'd help if you'd explain what you're trying to accomplish. But, you can exec it again from the Node repl or use the vm module in Node core: nodejs.org/api/vm.html Commented Feb 26, 2013 at 4:22

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.