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)); // 6Shorthand:
exportsexports.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.jsonEvery Node.js project uses a
package.jsonfile.
It describes the project and keeps track of dependencies.npm init -y //this command creates out package.json file -y means skip all questionsInstalling packages
npm install expressInstalls inside
node_modules/(node moules creates automatccally or run npm install )Adds to
dependenciesinpackage.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 packagespackage-lock.jsonAutomatically 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
dependenciesanddevDependenciesinpackage.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
npmandnpx?npm → Used to install/manage packages.
Example:npm install expressnpx → Used to execute packages without installing them globally.
Example:npx create-react-app my-app(runs directly without global install).
What is
package-lock.jsonand why is it important?→ See above
Without
package-lock.json, different environments might install slightly different versions, causing bugs.