Skip to content

Best way to check if some dependency exists in your package.json file.

License

Notifications You must be signed in to change notification settings

kristiyan-velkov/dependency-exists

Repository files navigation

dependency-exists

dependency-exists

Follow me Sponsors MIT License Node Version

Checks if an npm package exists in package.json dependencies. You can also provide a nice terminal message which can be customized as you like.

You can select where to search the npm package in package.json dependencies (by default is all of them):

  • dependencies
  • devDependencies
  • peerDependencies

Table of contents


Installation

  • Via yarn:
yarn add -D dependency-exists
  • Via npm:
npm install dependency-exists --save-dev

How to use?

API

Method Usage
dependencyExists() Еasy to use a method that will return a boolean value if npm package exists in package.json dependecies. Option to select specific dependecies scheck.
dependencyExistsWithText() Will return a boolean value if the npm package exists. Also will provide a nice terminal message which can be customized as you like.

  1. dependencyExists()
Properties Desrciption Type
packageName npm package name string
dependenciesConfig? Search in specific dependencies in package.json DependenciesConfig
interface DependenciesConfig {
  dependencies: boolean;
  devDependencies: boolean;
  peerDependencies: boolean;
}
function dependencyExists(packageName: string, dependenciesConfig: DependenciesConfig = {
        dependencies: true,
        devDependencies: true,
        peerDependencies: true
    }): boolean;
  • Default: By default accepts a string and returns a boolean value. Search in all pakage.json file dependencies:
    • dependencies
    • devDependencies
    • peerDependencies
import { dependencyExists } from "@kv/dependency-exists";

if (dependencyExists("some-npm-package-name")) {
  //=> true
} else {
  //=> false
}
  • With dependenciesConfig If you want to search for specific npm package in specific dependency, you can provide config:

    import { dependencyExists } from "@kv/dependency-exists";
    
    if (
      dependencyExists("some-npm-package-name", {
        dependencies: false,
        devDependencies: true, // Will search only in devDependencies
        peerDependencies: false,
      })
    ) {
      //=> true if the package exists in devDependecies
    } else {
      //=> false if the package NOT exists in devDependecies
    }

  1. dependencyExistsWithText() method
function DependencyExistsWithTextOptions(packageName: string, options: DependencyExistsWithTextOptions): boolean;
  • Info: Easy to use fully customizable method for checking if the npm package exists. Coming with default terminal response. The message in the console (color, text ) can be changed to whatever value you want.
Properties Desrciption Type
packageName npm package name string
options Custamizable terminal response. DependencyExistsWithTextOptions
export enum DefaultColorsEnum {
    success = "#008000",
    error = "#DC143C",
    warn = "#FFD700",
    info = "#00FFFF",
}

export interface DependencyExistsWithTextOptions {
    success: DependencyExistsWithTextOption;
    error: DependencyExistsWithTextOption;
}

interface DependencyExistsWithTextOption {
    color?: string | DefaultColorsEnum.success | DefaultColorsEnum.error | DefaultColorsEnum.info | DefaultColorsEnum.warn,
    text: string,
    warn?: TextOptionsStructure;
    info?: TextOptionsStructure;
}

interface TextOptionsStructure {
    color?: string | DefaultColorsEnum.success | DefaultColorsEnum.error | DefaultColorsEnum.info | DefaultColorsEnum.warn;
    text?: string;
}

// Example options types

 success: {
   text: "string",
   color?: "string",
    warn?: {
      color?: "string",
      text?: "string",
    },
    info?: {
      color?: "string",
      text?: "string",
    },
  },
  error: {
   text: "string",
   color?: "string",
    warn?: {
      color?: "string",
      text?: "string",
    },
    info?: {
      color?: "string",
      text?: "string",
    },
import { dependencyExistsWithText } from "@kv/dependency-exists";

dependencyExistsWithText("some-npm-package-name", {
  success: {
    color: "#ffffff", // string of terminal emulators supported hex color
    text: "npm package exists!", // string
    warn: {
      color: "#DEADED", // string of terminal emulators supported hex color
      text: "Some exists warning text.", // string
    },
    info: {
      color: "#00cafc", // string of terminal emulators supported hex color
      text: "Some exists info text!", // string
    },
  },
  error: {
    color: "#ffffff", // string of terminal emulators supported hex color
    text: "Example text: npm package NOT exist!", // string
    warn: {
      color: "#DEADED", // string of terminal emulators supported hex color
      text: "Some exists warning text.", // string
    },
    info: {
      color: "#00cafc", // string of terminal emulators supported hex color
      text: "Some exists info text!", // string
    },
  },
});

Examples:

Developer Support:

  • If you saw some issue/bug 🐛 related to the specific release version.
  • If you want some new feature or change to be added/implemented. 😊

Please, contact the creator of the replace-styles, so he will be able to fix or improve it:

Kristiyan Velkov

Email: [email protected]

linkedin

portfolio

Support my work

If you like my work and want to support me to work hard, please donate via:

Revolut Buy me a coffee
Krisityan Velkov - Revolut

Thanks a bunch for supporting me! It means a LOT 😍


Copyright ©️2023. All rights reserved.

About

Best way to check if some dependency exists in your package.json file.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published