Book Image

Instant Node Package Manager

By : Juzer Ali
Book Image

Instant Node Package Manager

By: Juzer Ali

Overview of this book

Node.js is the new buzz in town and has a vibrant community.It ships with npm, which is a tool that helps automate various development, deployment, and installation of node modules, and makes the process much easier. Instant Node Package Module is a practical guide to authoring and distributing node modules, and also for creating and managing private, standalone node.js projects. Starting with getting node.js installed and running on a variety of platforms, we then proceed to learn about code modularization and achieving it in node.js. Picking up a practical example, we will then explore the whole process and life cycle of conceiving, writing, and publishing a node module. We will also learn how to exploit a toolsincluded with npm to automate various development and deployment steps. Later on, we will discover the power of npm and how its different features and configurations can save large amounts of time by providing a helping hand for automating repetitive tasks between writing and publishing your modules.
Table of Contents (8 chapters)

So, what is Node Package Manager?


We have always known JavaScript as the language of the web. It is dynamically typed, implemented in C++ by most browsers, and provides API to interact with a web page. A simplistic way to describe node.js would be JavaScript that runs on the server-side. But that would be an understatement. node.js takes the JavaScript implementation to another level by augmenting it so that it can do pretty much everything that a server-side technology should be capable of doing. node.js can be used to:

  • Start an http/https server

  • Make http/https requests to other web servers

  • Start the TCP/UDP server or to make TCP/UDP requests

  • Read and write content on the filesystem

  • Spawn and manage new processes on operating system

The runtime of node.js is powered by Google's V8 JavaScript engine, whose task is to compile and run JavaScript code. V8 is the same engine that powers Google Chrome's JavaScript runtime.

The feature that separates node.js from other platforms is what is known as evented I/O. In node.js, during an I/O operation, whenever a chunk of data is read or written, an event is emitted. The programmer needs to register callbacks to react to these events, for example, to aggregate the chunks being read, or to determine whether the operation has been completed. This allows for making the I/O operations asynchronous, thus saving CPU cycles from being wasted. Other platforms make use of CPU time during I/O operations through multithreading. But an event-driven approach is much more efficient, because there is no thread spawning overhead and the memory footprint is much smaller.

To assist the evented I/O model, node.js has an event loop, which is constantly asking if there are there any more events to process. Whenever an event occurs, it gets registered into an event queue. The event loop executes the event handlers in the queue one by one. If there are no more events to process, the node.js runtime exits; of course, that is not true if you have started a server, in that case it will wait for events to occur.

To translate I/O operation into events, node.js uses libuv. libuv is a C++ library that provides infrastructure for evented I/O. The actual magic happens in the libraries written on top of libuv, node.js itself is just a thin JavaScript wrapper around these. Nonetheless, node.js is an excellent platform for writing programs involving large amount of I/O operations that scales really well.

Every mature platform has to face problems of modularization and redistribution of code. This is exactly the problem that Maven solved for Java, or the problem that Gems solved for Ruby, and thus npm for node.js.

We want to write JavaScript libraries and we want to be able to pass those libraries around. Anyone who wants to use those libraries, or what are being referred to as packages or node modules, should be able to install them with minimum number of steps possible. That is where node package manager (npm) comes in.

npm allows us to register our packages with a name so that we can import/export these packages using the registered name. Moreover, npm also integrates with the npm registry. The npm registry is a web service that hosts such packages. The standard npm registry is hosted at https://npmjs.org. It is accessible on open internet and is maintained by Joyent, the organization that sponsors node.js development. We can publish our packages on the npm registry, and it can be found on the npm registry website by its name. For example, an author has published his package by the name "express". We can obtain that package by running npm install express on our machine or by visiting http://npmjs.org/package/express. This way of distributing code over the internet is the reason why npm exists.

Previously, npm required separate installation. Recent versions of node.js ships with npm included.

Now that we know what node.js and npm are, in the next section we shall proceed to installing node.js on a computer.