Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Run node.js in interactive mode in terminal like "python -i …"?

Dumb question alert: I know that it is possible to run node.js in interactive mode like "python -i foo.py" where it runs the code in foo.py but then leaves you at the prompt ">>>" to interact, but how do I do it in node? I know it’s possible bc I’ve done before, just can’t remember how. Thank you

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

To run Node.js in interactive mode, you can use the -i or –interactive flag when starting the Node.js REPL (Read-Eval-Print Loop). The REPL is a command-line interface that allows you to execute JavaScript code and see the results in real time.

To start the REPL in interactive mode, open a terminal and run the node command with the -i flag:

node -i

This will start the REPL and print the > prompt, indicating that you can enter JavaScript code. You can then execute JavaScript code and see the results immediately. For example:

> 1 + 1
2
> const greeting = "Hello, world!"
undefined
> console.log(greeting)
Hello, world!
undefined

You can also run a JavaScript file in interactive mode by specifying the file name as an argument to the node command:

node -i foo.js

This will execute the code in the foo.js file, and then leave you at the REPL prompt so you can interact with the code that was run.

Note that the -i flag is only available in the latest versions of Node.js. If you are using an older version that does not support this flag, you can still enter interactive mode by running the node command without any arguments and pressing CTRL + C twice to exit the REPL.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading