๐ Introduction to Node.js and npm
Node.js is a runtime environment that allows you to run JavaScript code outside the browser โ commonly used for building backend services, command-line tools, and more. npm (Node Package Manager) is its companion tool used to manage libraries and dependencies.
โ๏ธ What is Node.js?
Node.js is built on the V8 JavaScript engine (the same engine used in Chrome) and allows developers to use JavaScript for server-side scripting.
- Runs JavaScript on the server
- Handles file systems, databases, HTTP, etc.
- Event-driven and non-blocking (asynchronous) architecture
// hello.js
console.log("Hello from Node.js!");
To run this file:
node hello.js
๐ฆ What is npm?
npm is the default package manager for Node.js. It helps you install and manage external packages (libraries), dependencies, and scripts.
- Install packages:
npm install axios
- Run custom scripts:
npm run start
- Manages your project with
package.json
๐ ๏ธ Installing Node.js & npm
- Go to https://nodejs.org
- Download the LTS version
- Run the installer (npm is included)
- Verify installation:
node -v npm -v
๐งช Creating Your First npm Project
mkdir my-app
cd my-app
npm init -y
This creates a package.json
file โ a manifest that stores your project's metadata and dependencies.
๐ Installing a Package
Example: Install axios
for making HTTP requests:
npm install axios
This adds a node_modules
folder and updates package.json
.
๐ Running a Script
Add this to your package.json
:
{
"scripts": {
"start": "node index.js"
}
}
Then run:
npm start