Open In App

What are Optional Dependencies and when should we use them ?

Last Updated : 25 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

What are Optional Dependencies? 

Dependencies are libraries on which a project depends to function properly.  

The term optional dependencies apply to dependencies that won’t cause a failure during the installation of an application or project since npm will ignore them if they fail.  Whether these dependencies are present or not, the application will still work as expected.

When should we use them?

  • For dependencies that may not work on every machine, you can use this as a fallback.
  • These dependencies are used mainly to reduce the transitive burden on libraries. For instance: If a library works with multiple different database types, but you typically only need one, then you can mark the library-dependent dependencies as optional so that the user can supply the one they prefer.
  • Adding a dependency as optional can speed up the installation process for Node projects.

Consider the following example for a better understanding of optional dependencies.

Consider a scenario where we want to include the Colors package as a dependency that helps us to take multiple logs and style them in the terminal. When you use console.log() in Node.js, your output will typically be white if your terminal has a dark background and black if your terminal has a light background. You do not have to worry about anything if you simply print a few short messages using console.log(). In contrast, if you print out a lot of text, it can be difficult to read. So how we can distinguish between the text on the console ?. The answer is we can use multicolor console output so that it is easy for the programmer to understand the console statements. 

Now, it is not necessary that this package would work effectively on all machines or that the user wants to use this package to style logs as well. In this case, Colors will be an Optional dependency. This dependency shouldn’t interrupt the working of the application, since its purpose was to display the logs.

How to install a dependency as an optional dependency: We can install a dependency as an optional dependency using the following command:

npm i package_name --save-optional

Does npm install optional dependencies: The npm will automatically install all packages listed as 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 

Initializing npm 

Step 2: Installing Colors package as an optional dependency. 

npm i colors --save-optional

Installing colors package

In package.json, your package will be listed under the “optionalDependencies” key.

package.json file 

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 

Javascript




// 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 GeeksforGeeks!!'.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:

Output: Using Optional dependencies 

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads