1. Node.js Basics and NPM

Ayush RajputAyush Rajput
3 min read

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.

  1. 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
     };
    
  2. 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
    
  3. Shorthand: exports

     exports.sayHello = function(name) {
       return `Hello, ${name}`;
     };
    
  4. 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:

    1. Install and manage third-party libraries (called packages).

    2. Manage your project dependencies via package.json.

    3. Share your own packages with others (publish to npm registry).

  1. 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
    
  2. Installing packages

     npm install express
    
    • Installs inside node_modules/ (node moules creates automatccally or run npm install )

    • Adds to dependencies in package.json.

  3. 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
    
  4. 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.

  5. NPM vs NPX

    • NPM → Installs a package.

    • NPX → Executes a package without installing globally.

INTERVIEW QUESTIONS

  1. What is the difference between dependencies and devDependencies in package.json?

    • dependencies → Packages required for the app to run in production.
      Example: express, mongoose.

    • devDependencies → Packages only needed during development.
      Example: nodemon, jest, eslint.

  2. Difference between npm and npx?

    • 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).

  3. 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.

0
Subscribe to my newsletter

Read articles from Ayush Rajput directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ayush Rajput
Ayush Rajput