SmartCodingTips

๐ŸŒ 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

  1. Go to https://nodejs.org
  2. Download the LTS version
  3. Run the installer (npm is included)
  4. 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
๐Ÿ’ก Tip: Node.js is not just for servers. You can build tools, APIs, and even desktop or full-stack apps using frameworks like Electron or Next.js.