Node.js is a runtime environment that allows you to execute JavaScript code on the server side, allowing for scalable network applications. NPM (Node Package Manager) is a package manager for JavaScript, enabling developers to share and reuse code.
Node.js enables you to run JavaScript code on the server side. It is built on Chrome's V8 JavaScript runtime and helps in building scalable network applications. Node.js operates on a single-threaded event loop and non-blocking I/O, allowing it to handle many connections simultaneously, making it suitable for building scalable and high-performance applications.
Here's a basic example of a HTTP server created using Node.js.
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!\n');
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`);
});
NPM, or Node Package Manager, is a package manager for JavaScript and the default package manager for Node.js. It allows developers to install, share, and manage dependencies (libraries and tools) in a project.
package.json is a crucial file in any Node.js project, serving as a manifest for your project. It holds metadata related to the project and lists its dependencies. Example of package.json:
{
"name": "example-project",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"express": "^4.17.1"
}
}
To install a package, you can use the following command:
npm install [package-name]
For instance, to install Express, a web framework for Node.js, you would run:
npm install express
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, World!');
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`);
});
Dependencies are the packages that your project relies on. NPM makes managing these dependencies straightforward. Here are the main types:
When you install a package using npm install [package-name] --save, NPM adds it to the "dependencies" in your package.json file. When you add --save-dev, it goes under "devDependencies".
NPM uses semantic versioning, or semver, to manage versions of packages. A semantic version is composed of three numbers separated by dots: major.minor.patch (e.g., 1.4.2).
You can specify the versions of your dependencies using symbols like ^ and ~. The caret ^ allows updates that do not modify the leftmost non-zero digit, and the tilde ~ allows patch-level changes for a specified minor version.
NPM scripts are used to automate tasks in your Node.js project, like starting your application or running tests. These scripts are defined in the scripts section of your package.json file.
"scripts": {
"start": "node index.js",
"test": "mocha test"
}
To run the `start` script, you would use the following command in your terminal:
npm start
NPM also enables developers to create and publish their own packages. To create a new package, you have to:
Node.js and NPM are foundational tools in modern JavaScript development for creating scalable, performant applications and managing dependencies. They offer a rich ecosystem of libraries and tools, enabling developers to build a wide range of applications efficiently. By leveraging Node.js and NPM, developers can harness the power of JavaScript beyond the browser, building server-side applications and contributing to the expansive JavaScript ecosystem.
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here