npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

detect-circular-deps

v3.0.0

Published

![detect-circular-deps](https://img.shields.io/npm/dt/detect-circular-deps.svg?style=flat-square) ![detect-circular-deps](https://img.shields.io/npm/v/detect-circular-deps.svg?style=flat-square) ![David](https://img.shields.io/david/abou7mied/detect-circu

Readme

Detect Circular Dependencies

detect-circular-deps detect-circular-deps David David

Detect Circular Dependencies problems that occurs while requiring a module

Circular Dependencies maybe necessary in some applications and it's not easy to avoid it.

This tools helps you to find Circular Dependencies that cause problems in your application.

Installation

npm i detect-circular-deps -g

What the problems this tool could detect?

Empty exports

Suppose you have two modules

a.js

const b = require('./b');

module.exports = {
  name: 'a',
};

b.js

const a = require('./a'); // a is empty {}

module.exports = {
  name: 'b',
};

And your entrypoint trying to

require("./a.js");

As you can see, when you require a.js, the a.js file is required again in b.js file and it will return empty object and that definitely will cause a problem in your application.

We can name this situation: alwaysEmptyExports

You can solve this issue by moving module.exports above the require statement

a.solved.js

module.exports = {
  name: 'a',
};

const b = require('./b.solved');

b.solved.js

module.exports = {
  name: 'b',
};

const a = require('./a.solved');

Missing Property

Suppose

a.js

const b = require('./b');

exports.name = 'a';

b.js

const a = require('./a');

// console.log(a.name); // = undefined
process.nextTick(() => {
  console.log(a.name); // = a
});

exports.name = 'b';

When you use exports. instead of module.exports, the exports are updated to the dependants but in the next tick.

If you need to detect that situation, we name that emptySyncAccess

If your file tried to access a property of another module in a circular dependency situation and could not get it (uncomment the third line of b.js), we name that: missingProperty

CLI

  Usage: detect-circular-deps [options] <file...>

  Options:

    -V, --version               output the version number
    -p, --problems              Report CD. that causing problems (Default)
    -c, --circular              Report all Circular Dependencies.
    -e, --always-empty-exports  Report CD. which its exports are always empty even when it's async-accessed after requiring (Causes Problems)
    -s, --empty-sync-access     Report CD. which its exports are empty only when it is sync-accessed after requiring. (May causes problems in future)
    -m, --missing-properties    Report CD. which some of the properties of its exports was sync-accessed after requiring but not found (Causes Problems)
    -d, --debug                 Print debugging messages
    -h, --help                  output usage information

Examples

Detect problems

detect-circular-deps examples/always-empty/a.js
✗  The exports of examples/always-empty/a is empty when it is required at examples/always-empty/b (It causes problems)
    Circular Path: examples/always-empty/a > examples/always-empty/b
detect-circular-deps examples/always-empty/a.solved.js
✓ No Problems for Circular Dependencies found! [filtered]

Detect any circular dependencies even it doesn't cause any problems

detect-circular-deps examples/always-empty/a.solved.js -c
✗  Circular requiring of examples/always-empty/a.solved
    Circular Path: examples/always-empty/a.solved > examples/always-empty/b.solved

Support es6 import/export

Now you can run the tool on es6 files

You need to add the flag --es6 to enable es6 modules.

The package babel-register is used to support es6 at runtime but you need to add .babelrc file to your working directory or any upper directory.

Example:

detect-circular-deps --es6 examples/es6/always-empty/a.js
Start detecting entrypoint: examples/es6/always-empty/a.js
✗  The exports of examples/es6/always-empty/a is empty when it is required at examples/es6/always-empty/b (It causes problems)
    Circular Path: examples/es6/always-empty/a > examples/es6/always-empty/b

Wildcard matching

This tool uses glob to match files using the patterns the shell uses but u need to qoute the pattern so that your terminal don't override it An example for running the tool on all files in this directory and sub directories

detect-circular-deps '*/**/*.js'

API

Functions

.problems({callback})

.circular({callback})

.alwaysEmptyExports({callback})

.emptySyncAccess({callback})

.missingProperties({callback})

All methods return a promise, also you can pass callback in the args

See examples/api for more info

Add to your tests

You can use the API in your tests to ensure that there is no circular dependencies in your application.

const detectCircularDeps = require('../index'); // replace with require("detect-circular-deps")

describe('Circular Dependencies Issues', () => {
  it('Should not cause a problem', async () => {
    const promise = detectCircularDeps.problems();
    require('../always-empty/a'); // you must require your module before the promise being resolved
    const results = await promise;
    if (results[0]) {
      throw new Error(results[0].message);
    }
  });
  it('Should not cause a problem', async () => {
    const promise = detectCircularDeps.problems();
    require('../always-empty/a.solved');
    const results = await promise;
    if (results[0]) {
      throw new Error(results[0].message);
    }
    process.exit(0);
  });
});

Limitations

This tool works only with node.js environment as it depends on the built-in node.js module package

I aim to make it works with webpack and browser applications.