Package Dependencies

List package dependencies.

Usage

var pkgDeps = require( '@stdlib/_tools/modules/pkg-deps' );

pkgDeps( file, [options,] clbk )

Asynchronously lists package dependencies.

pkgDeps( __filename, clbk );

function clbk( error, results ) {
    if ( error ) {
        throw error;
    }
    console.dir( results );
}

The first argument may be either an absolute file path string or an array of file paths.

pkgDeps( [ __filename, __filename ], clbk );

function clbk( error, results ) {
    if ( error ) {
        throw error;
    }
    console.dir( results );
}

Each file is represented by an object having the following fields:

  • file: file name.
  • deps: an array of package dependencies.

The function accepts the following options:

  • builtins: boolean indicating whether to include built-in package dependencies. Default: true.
  • walk: boolean indicating whether to walk relative module dependencies. Default: true.

By default, the function walks relative module dependencies to resolve both primary and secondary package dependencies. To only resolve a file's direct package dependencies, set the walk option to false.

var opts = {
    'walk': false
};

pkgDeps( __filename, opts, clbk );

function clbk( error, results ) {
    if ( error ) {
        throw error;
    }
    console.dir( results );
}

pkgDeps.sync( file[, options] )

Synchronously lists package dependencies.

var results = pkgDeps.sync( __filename );
// returns [...]

The function accepts the same options as pkgDeps() above.

Examples

var join = require( 'path' ).join;
var pkgDeps = require( '@stdlib/_tools/modules/pkg-deps' );

var file = join( __dirname, 'lib', 'index.js' );

pkgDeps( file, clbk );

function clbk( error, results ) {
    if ( error ) {
        throw error;
    }
    console.dir( results );
}

CLI

Usage

Usage: module-pkg-deps [options] [<file> <file> <file> ...]

Options:

  -h,    --help                Print this message.
  -V,    --version             Print the package version.
         --no-builtins         Do not include built-ins.
         --no-walk             Do not walk relative module dependencies.
         --split sep           Separator for standard input data. Default: '/\r?\n/'.

Notes

  • If the split separator is a regular expression, ensure that the split option is properly escaped.

    # Not escaped...
    $ echo -n $'file1\nfile2\n' | module-pkg-deps --split /\r?\n/
    
    # Escaped...
    $ echo -n $'file1\nfile2\n' | module-pkg-deps --split /\\r?\\n/
    
  • Results are printed to stdout as newline-delimited JSON (NDJSON).

Examples

$ module-pkg-deps /foo/bar/baz
{...}
$ echo -n $'/foo/bar/baz\n/beep/boop/bop\n' | module-pkg-deps --split /\\r?\\n/
{...}
{...}