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 Code
Node.js is a powerful JavaScript runtime environment that allows you to execute JavaScript code on the server side. In this post, we'll explore how to run Node.js scripts using the node command and the Node.js Read-Eval-Print Loop (REPL).
Running Scripts with Node Command
The node command is the most common way to execute Node.js scripts. To run a JavaScript file, open your terminal or command prompt, navigate to the directory containing the file, and use the node
command followed by the filename.
For example, consider a simple JavaScript file app.js
:
To run this script, use the following command:
This will execute the code in app.js
and output Hello, Node.js!
in the terminal.
Using the Node.js REPL
The Node.js REPL (Read-Eval-Print Loop) is an interactive shell that can be used to execute JavaScript code line by line. To start the REPL, simply type node
in your terminal without specifying a file, and press Enter.
Once in the REPL, you can enter JavaScript commands directly. For example:
The REPL is useful for testing small snippets of code quickly without having to create a separate file.
Conclusion
Running Node.js code is straightforward using either the node command or the REPL. Whether you're executing scripts from files or testing code snippets interactively, Node.js provides the tools you need to manage your code efficiently. In the next post, we will cover the basics of Node.js syntax.
Basics
- Previous
- Installation
- Next
- Syntax