noc

nocycle

Detect cycle `require` in node.js.

Showing:

Popularity

Downloads/wk

656

GitHub Stars

9

Maintenance

Last Commit

6yrs ago

Contributors

2

Package

Dependencies

0

Size (min+gzip)

0.6KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

Detect Cycle require

nocycle is a tiny node module to detect cycle require. If a file a.js requires b.js while b.js also requires a.js directly or indirectly, nocycle will find the cycle.

Install

Install this module through npm.

npm install --save nocycle

Usage

Simply call detect to start cycle detection.

// start to detect cycle require after `detect` is called.
require('nocycle').detect();

If there is any cycle require, above code will print something like following.

cycle require is detected. require stack is:
    * a.js
      b.js
    * a.js
      index.js

API

  • detect([printer])

Start to detect cycle require. If a cycle require is detected, printer will be called with current require stack.

Require stack is an array of required file absolute path names. Index 0 is the most recent required file path.

  • printer([logger])

Generate a default printer function which writes output to logger.

If logger is provided, printer will call it once to print cycle require stack.

Caveats

nocycle uses "black magic" to hack node.js' module loading process. It may be broken at anytime if node changes its implementation. Anyway, even if it's changed, there must be another black magic to implement it. I'll find out the way at that time.

FAQ

Q: I use a third-party module which has cycle require inside its code. I don't want to print cycle require information for it. How can I whitelist it?

A: Require the module before require('nocycle').detect().

For example, request has cycle require in one of its dependency. We need to write following code to whitelist request.

require('request');          // whitelist this module.
require('nocycle').detect(); // start to detect cycle require.

License

This module is licensed under the MIT license that can be found in the LICENSE file.

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100