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 🙏

© 2024 – Pkg Stats / Ryan Hefner

detect-import-require

v2.0.0

Published

list require and import paths from a JavaScript source

Downloads

705

Readme

detect-import-require

stable

This is like detective, but with a narrow focus and thin API, specifically aimed at supporting either import and/or require statements, and not much more.

Install

npm install detect-import-require --save

Example

Given the following file:

source.js

var foo = require('a').foo
var bar = require('./blah.js')
import { uniq } from 'lodash'
import { resolve } from 'path'
var fs = require('fs')
var detect = require('detect-import-require')

var src = fs.readFileSync('source.js', 'utf8')
console.log(detect(src))
//=> [ 'a', './blah.js', 'lodash', 'path' ]

See Custom AST for details on parsing additional language syntax, such as JSX or async/await.

Usage

NPM

modules = detect(src, [opt])

Returns an array of module names (require paths) from the given src String, Buffer or AST. By default, looks for import and require statements. Results are not de-duplicated, and are in the order they are found.

Options:

  • imports (Boolean) - whether to look for import statements, default true
  • requires (Boolean) - whether to look for require statements, default true

modules = detect.find(src, [opt])

Takes the same options as above, but returns an object with the following additional data:

{
  strings: [],
  expressions: [],
  nodes: []
}

Where strings is the array of module names, expressions is an array of expressions from dynamic require() statements, and nodes is an array of AST nodes for each found require/import statement.

Expressions do not appear in imports, and look like this:

[
  "path.join(__dirname, '/foo.js')",
  "__dirname + '/file.js'"
]

Custom AST

You can also pass a parsed AST, e.g. if you have a special build of acorn or want full control over parsing options. Here is an example with JSX:

var detect = require('detect-import-require')
var acorn = require('acorn-jsx');
var jsx = [
  "import 'foo';",
  "ReactDOM.render(",
    "<h1>Hello World</h1>,",
    "document.getElementById('root')",
  ");"
].join('\n');

var ast = acorn.parse(jsx, {
  ecmaVersion: 6,
  sourceType: 'module',
  allowReserved: true,
  allowReturnOutsideFunction: true,
  allowHashBang: true,
  plugins: {
    jsx: true
  }
})

detect(ast)
// => [ 'foo' ]

License

MIT, see LICENSE.md for details.