Import and Require Paths

List import and require paths.

Usage

var ls = require( '@stdlib/_tools/modules/import-require' );

ls( src )

Searches for import and require paths in a source string or Buffer.

var readFile = require( '@stdlib/fs/read-file' ).sync;

var src = readFile( __filename );
var results = ls( src );

console.dir( results );

The returned object has the following fields:

  • literals: an array of literal string import and require paths.
  • expressions: an array of import and require path expressions.

Notes

  • The function correctly resolves evaluated expressions:

    var ls = require( '@stdlib/_tools/modules/import-require' );
    var readFile = require( '@stdlib'+'/fs/read-file' ).sync;
    
    var src = readFile( __filename );
    var results = ls( src );
    /* returns
        {
            literals: [ '@stdlib/_tools/modules/import-require' ],
            expressions: [ '\'@stdlib\'+\'/fs/read-file\'' ]
        }
    */
    

Examples

var readFile = require( '@stdlib/fs/read-file' ).sync;
var ls = require( '@stdlib/_tools/modules/import-require' );

var file = readFile( __filename );
var results = ls( file );

console.log( 'Literals:' );
console.dir( results.literals );

console.log( 'Expressions:' );
console.dir( results.expressions );

CLI

Usage

Usage: import-require [options | src]

Options:

  -h,    --help                Print this message.
  -V,    --version             Print the package version.

Notes

  • May be used as part of a standard stream pipeline or TTY.

Examples

To pretty print results,

$ import-require "$(cat ./examples/index.js)"
$ cat ./examples/index.js | import-require

Example output:

Literals:
├── ./../lib/stdlib.js
└── ./../lib

Expressions:
└── '@stdlib'+'/fs/read-file'

To use as part of a pipeline,

$ cat ./examples/index.js | import-require | cat
{"literals":["./../lib/stdlib.js","./../lib"],"expressions":["\'@stdlib\'+\'/fs/read-file\'"]}