First App with Node
Create Folder and File
Create Folder
On Desktop, create a folder manually (e.g., NodeApp)
or
Open Terminal and run:
mkdir NodeApp
cd NodeApp
Open Folder in VS Code
Using VS Code directly
Open VS Code
Go to File > Open Folder
Select your NodeApp folder
Using Terminal
code .
This opens the current folder in VS Code (if code command is configured).Create a JavaScript File
- Create a file named
app.js
Node.js runs JavaScript files, so .js extension is required.Write Some Basic Code
Basic Output
console.log(20 + 20); // Output: 40
Function Example
function fruit(item) {
console.log("Fruit name is " + item);
}
fruit("apple");
Variable Example
var a = 10;
var b = 20;
console.log(a + b); // Output: 30
Run Node.js Code
Steps:
- Open terminal
- Navigate to your project folder (if not already)
- Run:
node app.js
File System Example (Just to Show)
var fs = require("fs"); // Import file system module
fs.writeFileSync("anil.txt", "My name is Anil");
This creates a file anil.txt and writes the content inside it.
REPL (Read Eval Print Loop)
- Just type
nodein terminal to enter REPL mode - Then type JS code line by line
> 2 + 2
4
> .exit