Introduction to NPM
If you haven't already set up your Node sample project, you can quickly do so by following these instructions
One of the tools (programs) that came with NodeJS when we installed it was npm, which stands for Node Package Manager. This program allows you to download all kinds of useful Node programs that you can use in your projects. These programs are called packages, or modules.
To install a module, you can run this command in the terminal:
npm install name-of-module-goes-here
When you install a module, it will be placed in a folder named node_modules. If you have not yet installed any modules for your project, then this folder will automatically be created for you.
We are going to install a module named gray-matter, which allows us to work with markdown files. Run this command in the terminal to install the gray-matter package/module (make sure to run this command from the project folder, which is node-sample-project):
npm install gray-matter
Note that a folder named node_modules has been created, the code for the gray-matter module was downloaded into this folder.
If you look inside the package.json file, you'll see that it keeps track of the modules you install for a project. It's ver important to keep a record of all the packages/modules that your app uses. These are known as dependencies because your app is dependent on them being installed in order to work properly.
For more information on the gray-matter package
Before we use the gray-matter module, we need to create a markdown file for us to experiment with. In the samples folder, create a file named sample.md and put this markdown code in it:
---
title: My Blog Post
author: John Doe
---
# This is my sample blog post
It will cover the following topics:
1. HTML
1. CSS
1. JavaScript
Now, in the samples folder, create a file named markdown-example.js and put this code into it:
const matter = require('gray-matter');
const obj = matter.read(__dirname + '/sample.md');
console.log(obj);
We'll discuss the code in a minute, but run the script by entering node samples\markdown-example.js in the terminal and note the output displayed.
It is an object that has a few properties. These are the ones we are interested in:
- The content property contains the markdown code (not including the meta data)
- The data property is an object that contains a property for each piece of meta data (in this case 'title' and 'author').
Now for the code, the first line in the script 'imports' the gray-matter module, which we are referring to as 'matter'. Note that if the path passed into the require() method does not start with a dot (.), then node will look for the module in the node_modules older.
The second line invokes read() method of the 'matter' object, which opens the file specified by the parameter that we've passed in. The read() method returns the object that we described above.
Now will install a package that will allow us to convert the 'content' into HTML.
Install a package/module named markdown-it by runnin this command:
npm install markdown-it
Now update markdown-example.js to look like this:
const matter = require('gray-matter');
const markDownIt = require("markdown-it")
const obj = matter.read(__dirname + '/sample.md');
console.log(obj);
const md = markDownIt({html:true});// html:true allows you to put HTML tags in the markdown files
const html = md.render(obj.content);
console.log(html);
The markdown-it module that is imported on the second line is a function, which we are referring to as markDownIt (modules may be objects, functions, classes, etc.). We call this function later in the script, and it returns an object (which we've named 'md'). This object has a render() method, which takes markdown as a parameter and returns HTML.
When you run the script again, you should see that the markdown code has been converted into HTML.
Note that the render() method, and the read() method that we called in the previous example are synchronous methods because they do not require you to pass in a callback parameter.