The Web Developer Bootcamp 2024

Colt Steele

Back to Node Index Page


 

Node NPM
What is NPM

npm is a package manager for the JavaScript programming language. npm is the default package manager for the JavaScript runtime environment Node.js and is included as a recommended feature in the Node.js installer.

It consists of a command line client, also called npm, and an online database of public and paid-for private packages, called the npm registry. The registry is accessed via the client, and the available packages can be browsed and searched via the npm website. The package manager and the registry are managed by npm, Inc.

Although "npm" is commonly understood to be an abbreviation of "Node Package Manager", it is officially a recursive backronymic abbreviation for "npm is not an acronym".

npm is essentially two things:

  1. a library of thousands of packages, (called the registry), published by other developers that we can use for free.
  2. a command line tool that we can use to easily install and manage those packages in our Node projects.

An in depth description and much more info about npm can be found here.
More information about npm packages and modules can be found here.
And the npm registry can be found here, where there is a searchable database to find new packages.

 

Using NPM

We're going to demonstrate installing and using node "packages" with two packages called "Jokes" and "Colors". We'll get them from the npm registry listed above. Here are the individual links:

Installing the npm packages

The first thing we need to do is create a folder for our jokes app, so we make a folder called [jokes].

To install the packages we can open the terminal app of our choice, or we can use the terminal in vs code. Which ever approach you take, you need to navigate to the newly created [jokes] directory, where we are going to install our packages.

If we look at the info for the give-me-a-joke package, you can see that we need to run [npm install --save give-me-a-joke] to install the package. Once done, we can go to the colors package and install it into our same [jokes] folder with [npm install colors].

We should now have our two packages installed. *note: I encountered a couple of errors on the colors installation, but when I tried the recommended fix, it broke the package, so on reInstall of the package, I ignored the errors.

Now we need the code to utilize the packages. First we'll set up the [jokes] package. We need a new "controller" file, so inside of our jokes directory, make a new file called [index.js]. Add this code to the newly created [index.js]:

  • JavaScript
  • index.js
  • // import the joke package
  • const jokes = require('give-me-a-joke');
  • // import the color package
  • const colors = require('colors');
  •  
  • // To get a random dad joke
  • jokes.getRandomDadJoke (function(joke) {
    • console.log(joke);
  • }

And now, if we run [node index.js] from the terminal (in the jokes directory), we should now see a random joke output to the console.

And if we look at the colors documentation, we can see various ways of outputting the text in different color formats. We are going to output the Joke in "green", so in our [index.js] file we are going to modify the joke output by modifying the console.log() statement, like so:

  • JavaScript
  • index.js
  • // import the joke package
  • const jokes = require('give-me-a-joke');
  • // import the color package
  • const colors = require('colors');
  •  
  • // To get a random dad joke
  • jokes.getRandomDadJoke (function(joke) {
    • console.log(joke.green);
  • }
and now when our joke outputs, it will have green text.

Now, bear in mind, we are using a node package to retrieve our joke. Packages are designed to run in a node environment. With node, we are communicating with the "backend" environment. Node modules are not design to work with the "frontend" environment. So we cannot use this process to display our joke on a webpage. To accomplish this we would simply run an api to retrieve the data and display the output. This process is meant to demonstrate the communication process, and how we can share files, folders, and packages for "backend" development.


Back to Top