Node Package Management

Background

Installing and managing project dependencies with NPM is such a breeze. The Node Package Manager and it’s accompanying tools makes working with dependencies such a trival exercise (arguably more pleasant than MSFT nuget packages and maven POM – later on these).

In a typical application, as a software programmer you will at least once encounter the horric nature of managing external and internal application dependencies, but truth be told, we have moved quiete far from the old urgly days when I first started writing code. Today you literally never have to worry much about such primitive issues using tools such as (not a complete list, but enough to give base and I’ll write about each one of them as time goes by)

  • Package Managers (NMP ,POM, Nuget fall in this category)
  • Inversion of Control (IoC) and Dependency Injection (DI)  (Springboot, Windsor Castle, Autofac, Unity etc)
  • Containarization with docker

However on today’s post, I’m going to be discussing the first category of these tools, specifically NPM, so let’s delve deeper.

Prerequsites

The barrier to entry for this topic is relatively low, there’s only a small finite basics that you need to either posess or attend to at least before going further, but I’ll try to bring you up to speed nonetheless.

Objectives

  • To install a basic npm package and use it within our application.

Key Results

  • Indentifying and discovering packages
  • Downloading and installing packages
  • Applying, embedding and utilizing  packages
  • Running a nodejs application

Guide

I tried to find the most simple but interesting npm package to use for this blog post and I came across a package called chalk which does terminal string styling.

The basics of creating a new node application, begins with creating a package.json file that describes the application metadata (simply information that describes data).

Step 1

using your terminal, navigate into a directory that you’d like to start working on: (On windows this could be your Document directory or even a simpler path like C:\work, if you’re on Unix like system you are probaly very familiar with traversing (simply navigating) directories ).

Step 2

Next create a project directory that will house all our application artifacts (loosely said, this could be anything related to the software, source files, configs etc)

In your terminal, type the below commands, in the order given. The first command will create a directory named “app1” in our current working directory, while the second will navigate into that directory.

mkdir app1 
cd app1

Step 3

We are going to use npm to create our first artifact (a package.json file) by answering a few basic questions to npm with the command

npm init

Answer the questions, you should have a similar outcome to mine

npm init

Step 4

Now that we have finished initializing our application using:

npm init

We can go ahead and install chalk package from npm using:

npm install chalk

After installing chalk, a new lock npm lock file called package-lock.json will also be generated in the same directory and the package.json will now be updated with a new element “dependencies”, depicting our recent added dependent “chalk”. The file should now be similar to the screenshot below

Chalk installed

Step 5

Now that we have installed the package, we can go a head and write our simple application to output the words “Hello World” on the console.

Create a new file “index.js” and populate it with this code and save the file.

const chalk = require('chalk');
const log = console.log;
log(chalk.blue.bold("Hello ") + chalk.green("World"));

Step 6

Now you can run your node application, using the command

node index.js

We should have a similar output to the screenshot below

And by that we would have completed this part of the tutorial.

I recommend that you play around with npm a bit more, add other simple node packages and play with them until you are at least more comfortable with this routing work.