Using Node.js 🔥
Checking Node Version
To check the installed version of Node.js, open your terminal or command prompt and run the following command:
node -vThis should display the current Node.js version installed on your system, e.g., v18.13.0. Ensure you have the latest stable version installed for optimal performance and compatibility.
Node REPL (Read-Eval-Print-Loop)
The Node REPL is an interactive environment that allows you to execute JavaScript code and see the results immediately. It's useful for testing code snippets and exploring language features.
To start the Node REPL, run the following command in your terminal:
nodeYou should see the > prompt, indicating you're in the REPL. Here, you can write JavaScript code, and it will be evaluated and printed. For example:
> 5 + 8
13
> let a = 3; a + 12
15To exit the Node REPL, use one of the following methods:
- Type .exit and press Enter.
- Press Ctrl + C twice.
Running Node.js Files
While the REPL is useful for testing, you'll typically write your Node.js code in separate files. Here's how to run a Node.js file:
1. Create a new file with a .js extension (e.g., index.js).
2. Write your JavaScript code in the file and save it.
3. Open your terminal and navigate to the directory containing the file using the cd command.
4. Run the file with the following command, replacing index.js with your file's name:
node index.jsNode.js will execute the code in the specified file, and you'll see the output (if any) in the terminal.
For example, if your index.js file contains:
console.log("Hello from Node");Running node index.js in the terminal will output:
Hello from Node🚀 With these basics covered, you're ready to explore more advanced Node.js concepts and build powerful server-side applications!