1. Node.js Basics and NPM

INTRODUCTION
Node.js is a runtime environment that allows you to run JavaScript outside the browser (on the server).
npm -v - check node version
//app.js
console.log("Hello, Node.js!");
// run command - node app.js
Output: Hello, Node.js!
NODE.JS MODULE SYSTEM
Node.js module system allows you to organize your code to multiple reusable pieces of modules.
In Node.js, every file is treated as a module.(like in react.js every file treated as component)
A module can export values (functions, objects, variables, classes, etc.).
Other modules can import them using
require
.
module.exports
- Used to define what a module exposes to other files.//math.js // Exporting a function function add(a, b) { return a + b; } // Exporting multiple things as an object function subtract(a, b) { return a - b; } module.exports = { add, subtract };
require
-Used to import from another module.const math = require("./math"); // ./ means local file console.log(math.add(5, 3)); // 8 console.log(math.subtract(10, 4)); // 6
Shorthand:
exports
exports.sayHello = function(name) { return `Hello, ${name}`; };
Node wrapper :
When you create a file in Node.js (say
app.js
), Node doesn’t run your code directly.
Instead, it wraps the code inside a special function before executing it.This wrapper looks like:
(function (exports, require, module, __filename, __dirname) { // Your actual code lives here });
NODE PACKAGE MANAGER(npm)
NPM stands for Node Package Manager.
It comes installed with Node.js.
It’s used to:
Install and manage third-party libraries (called packages).
Manage your project dependencies via
package.json
.Share your own packages with others (publish to npm registry).
package.json
Every Node.js project uses a
package.json
file.
It describes the project and keeps track of dependencies.npm init -y //this command creates out package.json file -y means skip all questions
Installing packages
npm install express
Installs inside
node_modules/
(node moules creates automatccally or run npm install )Adds to
dependencies
inpackage.json
.
Dependecy Management
npm install # Install all dependencies npm install express # Install specific package npm uninstall express # Remove package npm update # Update all packages npm outdated # Check outdated packages npm list # List installed packages npm view express # Show details about a package npm search react # Search packages
package-lock.json
Automatically generated when you install packages.
Locks the exact versions installed.
Ensures that every developer and server uses the same package versions, avoiding “works on my machine” issues.
NPM vs NPX
NPM → Installs a package.
NPX → Executes a package without installing globally.
INTERVIEW QUESTIONS
What is the difference between
dependencies
anddevDependencies
inpackage.json
?dependencies → Packages required for the app to run in production.
Example:express
,mongoose
.devDependencies → Packages only needed during development.
Example:nodemon
,jest
,eslint
.
Difference between
npm
andnpx
?npm → Used to install/manage packages.
Example:npm install express
npx → Used to execute packages without installing them globally.
Example:npx create-react-app my-app
(runs directly without global install).
What is
package-lock.json
and why is it important?→ See above
Without
package-lock.json
, different environments might install slightly different versions, causing bugs.
Subscribe to my newsletter
Read articles from Ayush Rajput directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
