0% found this document useful (0 votes)
11 views

Documents

My personal Documents

Uploaded by

NIKHIL BAWANE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Documents

My personal Documents

Uploaded by

NIKHIL BAWANE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Introduction to Node.

js

1. Introduction to Node.js

What is Node.js?
- Node.js is an open-source, cross-platform JavaScript runtime environment that
executes JavaScript code outside of a web browser.
- It's built on Chrome's V8 JavaScript engine, allowing JavaScript to be used for
server-side scripting.
- Key Features of Node.js:
- Asynchronous and Event-Driven: Non-blocking I/O operations, which makes it
efficient and scalable.
- Fast Execution: The V8 engine compiles JavaScript into native machine code,
which boosts performance.
- Single Programming Language: Developers can use JavaScript for both
frontend and backend, streamlining development.

Why Learn Node.js?


- Scalability: Ideal for building applications that need to handle many concurrent
connections, like chat apps or online games.
- Community and Ecosystem: Rich package ecosystem via npm, with thousands
of libraries available for various functionalities.
- Real-World Applications: Companies like Netflix, LinkedIn, Uber, and Walmart
use Node.js for its speed and scalability.

---

2. Environment Setup

Download and Install Node.js


- Visit the [Node.js official website](https://nodejs.org/).
- Two versions are available:
- LTS (Long Term Support): Recommended for most users due to stability.
- Current: For developers who need the latest features.
- Installation Process:
- Windows:
- Download the installer, run it, and follow the setup wizard.
- Select the default options unless you have specific needs.
- macOS:
- Download the `.pkg` file and run it.
- Alternatively, use a package manager like Homebrew: `brew install node`.
- Linux:
- Use the package manager for your distribution, e.g., `sudo apt-get install -y
nodejs` and `sudo apt-get install -y npm`.

Verify Installation
- Open the terminal (Command Prompt for Windows) and run the following
commands:
- `node -v` - Displays the Node.js version installed.
- `npm -v` - Displays the npm version installed.
- If both commands return version numbers, the installation was successful.

---

3. Understanding npm (Node Package Manager)

What is npm?
- npm (Node Package Manager) is the default package manager for Node.js. It
allows developers to install, update, and manage third-party packages (modules).
- Role of npm:
- Manage project dependencies.
- Distribute and share reusable code.
- Streamline project setup and configuration.

Basic npm Commands


- Initializing a Node.js Project:
- `npm init`: Initializes a new Node.js project, creating a `package.json` file.
- `npm init -y`: Skips the prompts and creates a `package.json` with default
values.

- Installing Packages:
- `npm install <package-name>`: Installs a package locally in the project
directory.
- `npm install -g <package-name>`: Installs a package globally, making it
available across projects.

- Managing Packages:
- `npm uninstall <package-name>`: Uninstalls a package from your project.
- `npm update <package-name>`: Updates a package to its latest version.

Creating a `package.json` File


- Purpose: The `package.json` file stores metadata about the project and lists
dependencies.
- Key Fields:
- `name`: The name of the project.
- `version`: The project version.
- `description`: A brief description of the project.
- `main`: The entry point of the application (e.g., `app.js`).
- `scripts`: Scripts to automate tasks (e.g., `"start": "node app.js"`).
- `dependencies`: Lists packages required to run the project.
- `devDependencies`: Lists packages required only for development.

Installing Dependencies
- Example: Installing Express.js, a popular web framework:
- Run `npm install express`.
- This command installs Express and adds it to the `dependencies` section in
`package.json`.
- node_modules Folder: All installed packages are stored here.
- Reinstalling Dependencies:
- If you clone a project or share it, run `npm install` to install all listed
dependencies from `package.json`.

---

4. First Node.js Script

Creating a Simple Node.js Application


- Step 1: Create a new directory for your project and navigate into it:

mkdir my-first-node-app
cd my-first-node-app

- Step 2: Initialize the project:

npm init -y

- Step 3: Create a file named `app.js`:


javascript
// app.js
console.log("Hello, Node.js!");

- Step 4: Run the script using Node.js:

node app.js

- Output: You should see `Hello, Node.js!` printed in the terminal.

Understanding `require` and `module.exports`


- `require`: Used to include modules (both built-in and third-party) in your
application.
javascript
const fs = require('fs'); // Include the File System module

- `module.exports`: Allows you to export functions, objects, or values from a


module so they can be used in other files.
javascript
// math.js
function add(a, b) {
return a + b;
}

module.exports = { add };

// app.js
const math = require('./math');
console.log(math.add(2, 3)); // Output: 5
Introduction to `console.log` for Output
- `console.log`: A simple way to output messages to the console.
- Encourage students to experiment with logging different data types: strings,
numbers, arrays, objects, etc.
- Example:
javascript
console.log("Hello, World!");
console.log(42);
console.log([1, 2, 3]);
console.log({ name: "Alice", age: 25 });

---

You might also like