External NodeJS modules

You can use external modules by using npm. Before you can do that you will need to create a package.json file. You can create one using default values the npm init -y command. Look at the content of the package.json file.

Let’s use the chalk module to display a nice colorful greeting. Let’s modify your greet module.

Install chalk like this:

npm install --save chalk

Using the --save option will add an entry to your package.json file:

"dependencies": {
    "chalk": "^1.1.3"
  }

Using the package.json will allow you and others to install the external dependencies easily in the future.

Change greet_xola.js to look like this;


const chalk = require('chalk');
//import the greet module that is in the current folder
const greet = require('./greet');

const styledMessage = chalk.bgGreen.black(greet('Xola'));
console.log(styledMessage)

Run it using: node greet_xola.js - you should see a colorful greeting.

Now use the figlet module to create a greeting in ASCII art.