What it really means is that Node.js is not a silver-bullet new platform that will dominate the web development world. Instead, it’s a platform that fills a particular need. And understanding this is absolutely essential. You definitely don’t want to use Node.js for CPU-intensive operations; in fact, using it for heavy computation will annul nearly all of its advantages. Where Node really shines is in building fast, scalable network applications, as it’s capable of handling a huge number of simultaneous connections with high throughput, which equates to high scalability.
How it works under-the-hood is pretty interesting. Compared to traditional web-serving techniques where each connection (request) spawns a new thread, taking up system RAM and eventually maxing-out at the amount of RAM available, Node.js operates on a single-thread, using non-blocking I/O calls, allowing it to support tens of thousands of concurrent connections held in the event loop.
To handle the packages, you will need a package manager. Most commonly used package managers include npm, yarn and pnpm.
When discussing Node.js, one thing that definitely should not be omitted is built-in support for package management using NPM, a tool that comes by default with every Node.js installation
npm is the default package manager and is most widely used. It is perfect for beginners and has terrific support. The previous versions had two main issues:
- Initially, npm didn’t have a lock file. The exact versions of each dependency weren’t precisely stated. The code would be run with the latest versions which caused compatibility problems.
- Npm wasn’t deterministic — different versions of packages were allowed on different machines.
Yarn fixed these problems and attracted a massive audience. Later, npm updates fixed the issues and made it as usable as Yarn.
Yarn was built by Facebook with the goal to fix scalability issues npm had at the time. Three core principles of yarn are speed, reliability, and security. Yarn can install packages faster by using local caching — it is much faster to grab a package from cache memory than to wait for the download. Local caching also enables offline mode — packages in cache can be directly installed. Yarn was first to feature a file with locked package versions so the code wouldn’t be forcibly run on the latest version. Security is ensured with the use of checksums before execution.
There are some unique features of yarn which make it faster than npm. Installation of packages is parallel, while npm installs sequentially. Yarn guarantees that dependencies will be installed the same way on a different machine, regardless of the install order.
If we have ten projects which use the same ten packages, each project will have its own copy in node_modules. This practice isn’t very efficient in terms of disk space. pnpm solves this problem by installing only one copy of the package on disk. The node_modules directory will only contain a hard link to the location of the package on the disk. Developers of the package manager claim it is as fast as yarn and npm — in some cases even faster. pnpm is strict in the sense that it only accesses the dependencies specified in package.json. It is great for multi-package repositories (monoreps).
Rush technically isn’t a package manager, but it allows you to pick out whether you want to use npm, yarn or pnpm. It enforces that the same version of the package manager is used by all team members.
Some of the most useful npm modules today are:
- express – Express.js—or simply Express—a Sinatra-inspired web development framework for Node.js, and the de-facto standard for the majority of Node.js applications out there today.
- hapi – a very modular and simple to use configuration-centric framework for building web and services applications
- connect – Connect is an extensible HTTP server framework for Node.js, providing a collection of high performance “plugins” known as middleware; serves as a base foundation for Express.
- socket.io and sockjs – Server-side component of the two most common websockets components out there today.
- pug (formerly Jade) – One of the popular templating engines, inspired by HAML, a default in Express.js.
- mongodb and mongojs – MongoDB wrappers to provide the API for MongoDB object databases in Node.js.
- redis – Redis client library.
- forever – Probably the most common utility for ensuring that a given node script runs continuously. Keeps your Node.js process up in production in the face of any unexpected failures.
- bluebird – A full featured Promises/A+ implementation with exceptionally good performance
|v, –version||It is used to print node’s version.|
|-h, –help||It is used to print node command line options.|
|-p, –print “script”||It is identical to -e but prints the result.|
|-c, –check||Syntax check the script without executing.|
|-i, –interactive||It opens the REPL even if stdin does not appear to be a terminal.|
|-r, –require module||It is used to preload the specified module at startup. It follows require()’s module resolution rules. Module may be either a path to a file, or a node module name.|
|–no-deprecation||Silence deprecation warnings.|
|–trace-deprecation||It is used to print stack traces for deprecations.|
|–throw-deprecation||It throws errors for deprecations.|
|–no-warnings||It silence all process warnings (including deprecations).|
|–trace-warnings||It prints stack traces for process warnings (including deprecations).|
|–trace-sync-io||It prints a stack trace whenever synchronous i/o is detected after the first turn of the event loop.|
|–zero-fill-buffers||Automatically zero-fills all newly allocated buffer and slowbuffer instances.|
|–track-heap-objects||It tracks heap object allocations for heap snapshots.|
|–prof-process||It processes V8 profiler output generated using the v8 option –prof.|
|–V8-options||It prints V8 command line options.|
|–tls-cipher-list=list||It specifies an alternative default tls cipher list. (requires node.js to be built with crypto support. (default))|
|–enable-fips||It enables fips-compliant crypto at startup. (requires node.js to be built with ./configure –openssl-fips)|
|–force-fips||It forces fips-compliant crypto on startup. (cannot be disabled from script code.) (same requirements as –enable-fips)|
|–icu-data-dir=file||It specifies ICU data load path. (Overrides node_icu_data)|