Basics
Node.js Running Code
Running Node.js Scripts
Node.js scripts run via node command or REPL with .js files.
Introduction to Running Node.js Scripts
Running Node.js scripts is a fundamental skill for any developer working with Node.js. In this guide, we'll explore the primary ways to execute Node.js code: using the node command and the Node.js Read-Eval-Print Loop (REPL).
Running Scripts with the Node Command
The most common way to run a Node.js script is by using the node
command followed by the filename. This approach requires a JavaScript file with the .js
extension.
To execute this script, open your terminal and navigate to the directory containing app.js
. Then run the following command:
This command will output Hello, Node.js!
to the console, demonstrating a successful execution of your Node.js script.
Using the Node.js REPL
The Node.js REPL (Read-Eval-Print Loop) is an interactive shell that allows you to execute JavaScript code line-by-line. It's useful for testing small code snippets and debugging.
After entering the REPL, you can type JavaScript code directly. For example, to print a message:
The REPL will immediately execute your code and display the output:
Welcome to the Node.js REPL!
Conclusion
Running Node.js code is straightforward, whether you use the node
command with a JavaScript file or the interactive REPL. Mastering these techniques is an essential step for any Node.js developer.
Basics
- Previous
- Installation
- Next
- Syntax