Contents
Understanding the Basics
When getting started with Node.js, it’s important to understand how to write and run basic scripts, as well as how to use the Node.js REPL (Read-Eval-Print Loop) for quick experimentation and debugging. In this section, you’ll learn how to write your first “Hello World” application, run Node.js scripts from the command line, and explore the Node.js REPL.
Writing Your First “Hello World” Application
The “Hello World” application is the most basic example you can write when learning a new programming language or environment. In Node.js, this involves creating a simple script that outputs “Hello, World!” to the console.
Step 1: Create a New File
- Open your text editor (e.g., VS Code).
- Create a new file named
hello.js
in your project directory.
Step 2: Write the “Hello World” Script
In the hello.js
file, add the following code:
console.log("Hello, World!");
This script uses Node.js’s console.log()
function to print “Hello, World!” to the console.
Running Node.js Scripts from the Command Line
Once you’ve written your first Node.js script, you can run it from the command line.
Step 1: Open Your Terminal or Command Prompt
- Open the terminal (macOS/Linux) or command prompt (Windows).
- Navigate to the directory where your
hello.js
file is located using thecd
command.
Step 2: Run the Script
Run the script using the node
command:
node hello.js
When you run this command, Node.js executes the hello.js
file, and you should see the output:
Hello, World!
This confirms that your script ran successfully.
Introduction to the Node.js REPL (Read-Eval-Print Loop)
The Node.js REPL is an interactive shell that allows you to write and execute JavaScript code in real-time. It’s a useful tool for experimenting with JavaScript and testing small snippets of code.
Step 1: Start the Node.js REPL
To start the REPL, simply open your terminal or command prompt and type:
node
You’ll see a prompt that looks like this:
>
This prompt indicates that the REPL is ready to accept your input.
Step 2: Experiment with JavaScript in the REPL
You can now type JavaScript code directly into the REPL and see the results immediately. For example:
> console.log("Hello, World!");
Hello, World!
undefined
Here’s what happens:
- You entered
console.log("Hello, World!");
. - The REPL executed the code and printed
Hello, World!
to the console. - The
undefined
output indicates thatconsole.log()
doesn’t return a value.
You can also perform other JavaScript operations, such as:
Basic Arithmetic:
> 5 + 3
8
Variable Declaration and Usage:
> let name = "Node.js";
undefined
> name
'Node.js'
Step 3: Exit the REPL
To exit the REPL, press Ctrl+C
twice or type .exit
and press Enter
:
> .exit
This will return you to the regular terminal prompt.
Conclusion
Writing your first “Hello World” application in Node.js, running Node.js scripts from the command line, and using the Node.js REPL are foundational skills that will help you get comfortable with Node.js development. The REPL is particularly useful for quickly testing code snippets and understanding how Node.js and JavaScript work in real-time. With these basics under your belt, you’re ready to start exploring more advanced features of Node.js and building more complex applications.
Related Chapters
- What is Node.js?
- Setting Up the Development Environment
- Understanding the Basics
- Node.js Modules
- Working with the File System
- Node.js Package Manager (npm)
- Asynchronous Programming in Node.js
- Building a Web Server with Node.js
- Introduction to Express.js
- Working with Databases
- Authentication and Security
- RESTful APIs with Node.js
- Testing in Node.js
- Planning the Project
- Developing the Backend with Node.js
- Developing the Frontend
- Deployment