Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

detect-import-require

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

detect-import-require

like the detective module, but for CommonJS + imports

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
64
decreased by-39.05%
Maintainers
1
Weekly downloads
 
Created
Source

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' ]

Usage

NPM

modules = detect(src, [opt])

Returns an array of module names (require paths) from the given src String or Buffer, which is assumed to be ES6/ES5. 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'"
]

License

MIT, see LICENSE.md for details.

Keywords

FAQs

Package last updated on 18 Nov 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc