Node JS

Node JS

What is NODE JS

  • Browser can only undersntand HTML , CSS and JavaScript

  • That means to run JavaScript we need a browser

  • But what if we want to run JavaScript out of the browser , then we need a runtime environment that is able to perfrom memory management , event handling and other tasks

  • For this purpose we use Node JS

NODE JS

  • Node js is a runtime environment that helps to run javaScript ouf of the browser

  • But why do we need to run javascript out of the browser

    • What if we are writing a server side code that is in JavaScript , then we need NODE js to be installed on that server as well

    • What is a server

      • A server is basically a computer that has extreme processing capabilites like huge amoung of RAM , SDD

      • Hence if we are using js to manage the server then to undersntand JS we need a runtime environment which is node js

  • Why is it called as NODE ?

    • Node.js is called "Node" because it was designed to be a scalable, event-driven system that can serve as a "node" in a network, handling numerous connections simultaneously
  • Even in the logo itselsf it says`` RUN JAVASCRIPT EVERYWHERE

When you INSTALL node what all gets installed

  • When you install Node.js locally, typically through an official installer or a package manager, several components and tools are installed. Here’s an overview of what gets installed:

1. Node.js Runtime

  • Node.js Binary: The core executable that runs JavaScript code outside of a browser environment.

  • Built-in Libraries: Standard libraries that come with Node.js, such as fs (file system), http, path, and more.

2. npm (Node Package Manager)

  • npm Binary: The command-line tool for managing JavaScript packages.

  • Node_modules Directory: A folder where installed packages and their dependencies are stored. This directory is created inside your project directory when you install packages.

3. Core Utilities

  • npx: A package runner tool that comes with npm. It allows you to execute binaries from npm packages without installing them globally.

  • Node-gyp: A cross-platform command-line tool written in Node.js for compiling native addon modules for Node.js.

4. Configuration Files

  • package.json: A file that holds metadata relevant to the project and lists the dependencies. It is created when you run npm init.

  • package-lock.json: Automatically generated for any operations where npm modifies either the node_modules tree or package.json. This file ensures that installs are repeatable and predictable.

Node Package Manager

npm, which stands for Node Package Manager, is a crucial tool for managing JavaScript packages and dependencies. It is installed automatically when you install Node.js. Here’s a detailed overview of what npm is and what it does:

Key Features and Functions of npm

  1. Package Management:

    • Install Packages: You can install JavaScript libraries and frameworks from the npm registry into your project. For example, to install the express package:

        npm install express
      
    • Uninstall Packages: To remove an installed package:

        npm uninstall express
      
  • By default the node terminal is configured to look up repository in https://registry.npmjs.org/

  • We can set a custome registry as well :

    
      npm config set registry https://custom-registry.example.com/
    

    npm config set registry https://custom-registry.example.com/

  • Dependency Management:

    • package.json: This file lists the dependencies and metadata of your project. When you run npm install, npm reads this file to determine which packages to install.

    • package-lock.json: This file ensures that installs are repeatable and consistent across different environments by locking the versions of installed packages.

  • Script Management:

    • You can define scripts in your package.json to automate common tasks, such as running tests or starting your application. For example:

        "scripts": {
          "start": "node app.js",
          "test": "mocha"
        }
      
    • To run a script:

        npm run start
      
  • Global vs. Local Installation:

    • Local Installation: Installs the package into the node_modules directory within your project. These packages are accessible only within the project.

        npm install express
      
    • Global Installation: Installs the package system-wide, making it accessible from any project. Useful for command-line tools.

        npm install -g nodemon
      
  • Publishing Packages:

    • You can publish your own packages to the npm registry for others to use. To publish a package:

        npm publish
      
    • Before publishing, you need to create an account on the npm website and log in using:

        npm login
      

Commonly Used npm Commands

  • Initialize a Project:

      npm init
    

    This command creates a package.json file with project details.

  • Install All Dependencies:

      npm install
    

    Installs all packages listed in the package.json.

  • Update Packages:

      npm update
    

    Updates all the packages to the latest version respecting the version ranges specified in package.json.

  • List Installed Packages:

      npm list
    

    Lists all installed packages and their dependencies.

  • Check for Outdated Packages:

      npm outdated
    

    Shows a list of packages that are outdated.

  • Audit Packages for Vulnerabilities:

      npm audit
    

    Runs a security audit and provides a report of vulnerabilities in the dependencies.

Example Workflow

  1. Create a new project directory and initialize npm:

     mkdir my-app
     cd my-app
     npm init -y
    
  2. Install Express and Save to Dependencies:

     npm install express --save
    
  3. Create a simple app.js file:

     // app.js
     const express = require('express');
     const app = express();
     const port = 3000;
    
     app.get('/', (req, res) => {
       res.send('Hello World!');
     });
    
     app.listen(port, () => {
       console.log(`App listening at http://localhost:${port}`);
     });
    
  4. Add a start script to package.json:

     "scripts": {
       "start": "node app.js"
     }
    
  5. Run the application:

     npm run start