akst
akst

Reputation: 956

How to find a locally installed npm module which depends on a certain module?

There's an error being thrown in of my node.js projects, and the stack trace doesn't seem to point back to any of the libraries I'm using, is there a simple method of finding out what packages depend on this package in my node_modules directory?

Ideally this method doesn't involve manually checking the package.json of every module in my node_modules directory.

Upvotes: 0

Views: 50

Answers (2)

user663031
user663031

Reputation:

Use npm ls <module-name>.

Upvotes: 1

akst
akst

Reputation: 956

You can use a bit of bash scripting to automate this

who_depends_on() {
    local dependency=$1;
    for file in $(ls node_modules); do
        local match=$(grep $dependency "node_modules/$file/package.json");
        if [[ $match ]]; then
            echo "'$file' is dependant in '$dependency'";
        fi
    done
}

Put the function wherever you store your shell functions, and then run like so

who_depends_on "your-package-here"

The main caveat with this package is it'll pick up the package.json package you're looking for as well.

Upvotes: 0

Related Questions