Node.js is an open-source, cross-platform JavaScript runtime environment that allows us to execute JavaScript outside a web browser.
We can use Node.js to create web servers, backend applications, REST APIs, command-line tools, and real-time applications.
Normally, JavaScript runs inside web browsers such as Google Chrome. Node.js uses Chrome's V8 JavaScript engine to execute JavaScript programs on a computer or server.
Node.js provides an environment for executing JavaScript code without needing a web browser.
Node.js can handle server-side operations, process requests, communicate with databases, and send responses to clients.
Node.js can run on Windows, macOS, and Linux.
Node.js supports asynchronous operations. It can start an input/output operation and continue other work while waiting for that operation to finish.
Node.js can respond to events such as incoming requests, completed operations, and user-defined events.
NPM (Node Package Manager) helps developers install and manage reusable packages for their projects.
Node.js provides built-in modules such as fs,
path, http, and os
for working with files, paths, servers, and operating-system information.
| JavaScript in Browser | JavaScript in Node.js |
|---|---|
| Runs inside a web browser. | Runs outside the browser. |
| Can manipulate webpage HTML using the DOM. | Can access Node.js APIs such as the file system. |
| Commonly used for frontend development. | Commonly used for backend development. |
Uses browser APIs such as document. |
Uses APIs such as fs and http. |
NPM stands for Node Package Manager. It is commonly used to install, update, and manage packages in Node.js applications.
Run the following command in your terminal:
npm install express
Express.js is a popular framework for creating web servers and APIs with Node.js.
After installing Node.js, open Command Prompt or a terminal and enter the following commands.
node -v
This command displays the installed Node.js version.
npm -v
This command displays the installed NPM version.
Let's create a simple program that displays a message in the terminal.
Create a file named app.js.
console.log("Hello, Node.js!");
Open the terminal in the folder containing the file and run:
node app.js
Hello, Node.js!
console.log() displays information in the terminal."Hello, Node.js!" is the message to display.node app.js executes the JavaScript file using Node.js.We can also use JavaScript variables in Node.js.
let studentName = "Rahul";
let studentAge = 20;
let course = "Node.js";
console.log("Student Name:", studentName);
console.log("Student Age:", studentAge);
console.log("Course:", course);
Student Name: Rahul
Student Age: 20
Course: Node.js
node -v.npm -v.app.js.console.log().1. What is Node.js?
2. Which JavaScript engine does Node.js use?
3. Which command runs app.js using Node.js?