Node - rohit120582sharma/Documentation GitHub Wiki

Node.js (Node) is an open source runtime environment.

C++ has many features that let it interact with the OS directly; JavaScript does not! So it has to work with C++ to control these computer features and this combination is known as Node.

Node.js has a ton of built-in modules (fs, http, crypto, zip, ...) providing rich features through easy-to-use asynchronous APIs. These modules trigger Node features that are built-in C++ to use our computer’s internals via Libuv.

Node.js has two major dependencies Google JavaScript V8 engine and Libuv.

  • V8 - It is a powerful open-source Javascript engine provided by Google. The actual purpose of this project is to execute the javascript code outside the browser.

  • Libuv - The Libuv project is the open-source project written totally in C++(100%). The purpose of this project is to give access to the Node, the operating system file system. It gives us access to networking and also handles some aspects of concurrency as well.

    JS » Node » Libuv » Computer feature (e.g. network, file system)

Process.binding()/internalBinding() is the bridge between the JS side of Node and C++ side of Node which is where the lot of internal work that the Node does for you is actually implemented. Lot of your code is ultimately relies on C++ code. The binding does all the heavy lifting.


Some other characteristics:

  • It is not a framework or web-framework or language.
  • It is the cross-platform environment and can run on OS X, Windows, Linux, and IBM etc.
  • It provides an event-driven architecture and non blocking I/O that is optimised and scalable.
  • It is not for Multi-Threaded applications. It follows Single Thread with Event Loop architecture.
  • It uses npm as a package manager to handle dependencies.
  • It uses CommonJS for its module loader. Using require we can get access to built in and 3rd party npm modules.

We can use Node Platform to develop the following kind of applications:

  • Tooling (build, automation, etc)
  • APIs (REST, GraphQL, Realtime, etc)
  • Shareable libraries
  • Desktop applications

References



Node Platform Components

Node CLI

Node.js Platform has a CLI (Command Line Interface) to run basic commands and also script files. When we install Node, by default we will get this component.

There are two modes to use the Node command:

  • REPL:
    • Node comes with virtual environment called REPL (aka Node shell) which stands for Read-Eval-Print-Loop. REPL can be started by simply running following command on shell.
    • The common way of starting the Node.js REPL is invoking the node command with no arguments.
    • The Node.js REPL also loads all the standard libraries in the global context
    • There are some special commands that you can send to the REPL. These commands start with a dot:
      • The .exit command finishes the REPL session
      • The .editor command is quite useful to type multi-line content
      • The .help command gives a list with the available commands
    $ node
  • Execute Script:
    • Execute a script by using a single file path argument.
    $ node filename.js

NPM

NPM (Node Package Manager) is the package manager for the JavaScript platform. When you install Node, npm is automatically installed.

It is a command line interface program to manage Node.js libraries. It revolutionised the way JavaScript developers work.

It is all about code-sharing and re-usability. It enables JavaScript developers to do three main things:

  • Share their code with other developers
  • Re-use own code in other projects
  • Use code written by others in their projects

NPM uses Semantic Versioning (SemVer) format for packages versioning.


Package.json

It is a plain JSON(Java Script Object Notation) text file which contains all metadata information about Node project / application.

Every Node.js Package or Module should have this file at root directory to describe its metadata in plain JSON Object format.

NPM (Node Package Manager) uses this package.json file information about Node.js Application information or Node.js Package details.

This file contains two mandatory directives

  • name: it is a unique Node.js Package (Module) name or our Node.js Project name. Without this directive, we cannot install our Node Package by using NPM.
  • version: it is the Node.js Package version number. NPM uses this version number to install or uninstall or update the right package in our Node Environment.
⚠️ **GitHub.com Fallback** ⚠️