Steps to Install and Use Optional Dependencies
Installing Colors package as an optional dependency: As an example, we’ll install the colors package as an optional dependency.
Step 1: Initialize npm
Make a new project directory, go to your terminal, and initialize npm by using the following command.
npm init -y
Step 2: Installing Colors package as an optional dependency.
npm i colors --save-optional
In package.json, your package will be listed under the “optionalDependencies” key.
Note: However, not all dependencies can be made optional. When a dependency is made optional, your program is still responsible for handling its absence.
Step 3: Make a new file “app.js” and write the following code in it.
To use the colors module, first, we need to import it into our app.js file, just like we do for the other dependencies.
const colors = require('colors');
Then we will set themes for errors and warnings using colors.setTheme and pass an object to it.
colors.setTheme({
warn: 'yellow',
error: 'red'
});
Now we are ready to work with the different styles using:
console.log('Enter your text'.color/pattern)
Example: We will use a variety of colors and backgrounds. Our text will be bold, italicized, and use patterns such as rainbows. Random patterns generate different colors and patterns each time. Rainbow patterns produce rainbow-colored text.
// app.js
// Using colors package as an optional dependency
const colors = require('colors');
// Setting themes
colors.setTheme({
warn: 'yellow',
error: 'red'
});
// Outputs a rainbow colored text
console.log('-----------------------------------------'.rainbow);
// Outputs a yellow , bold text
console.log('Hello,geeks'.yellow.bold);
// Outputs a black colored text with a bright green background
console.log('Welcome to w3wiki!!'.black.bgBrightGreen);
// Outputs a green underlined text
console.log('A Computer Science Portal for geeks'.green.underline);
// Outputs a italicised cyan colored text
console.log('You can watch tutorials.'.cyan.italic);
// Outputs an error
console.log("Oops! Currently , you don't have any courses in your account".error);
// Outputs a bold and bright magenta colored text
console.log("You can read articles".brightMagenta.bold);
// Outputs a rainbow colored text
console.log('CONTRIBUTE'.rainbow);
// Outputs a warning
console.log('Please login to contribute'.warn);
// Outputs a random pattern
console.log('Thanks for visiting gfg'.random);
// Outputs a rainbow colored text
console.log('-----------------------------------------'.rainbow);
Step to run the application: Run the application using the following command :
node app.js
Output:
We have made our output logs more readable by displaying them distinguishably.
When we wish to use a dependency but do not want our application to break in the event of an installation failure, we can place it in an optional dependency. Although it logs a few statements, the application might not crash without the Colors package.
What are Optional Dependencies and when should we use them ?
Optional dependencies in Node.js offer a flexible way to enhance the functionality of a package or module by providing additional features or optimizations without being mandatory for its core functionality. In this article, we’ll delve into what optional dependencies are, how they work, and when to use them effectively.