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

get-es-imports

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

get-es-imports

Recursively get imported files and respective import names for files using ES import/export syntax

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

get-es-imports

Recursively or non-recursively gets all ES6 imports used within a project.

E.g.

// a.js
import b from './b';
...
// b.js
import * as c from './c';
...
// c.js
import { find } from 'lodash';
...

Returns an object in the form:

{
  'full path of ./b': ['default'],
  'full path of ./c': ['*'],
  'full path of lodash': ['find'],
}

API

import getEsImports from 'get-es-imports';

const { dependencies, loadedFiles } = await getEsImports({
  files = [],
  recurse = true,
  exclude = [],
  parser = defaultParser,
  parserOptions = defaultParserOptions,
  resolveOptions = {},
});

files is an array of paths for JS files you want to check.

The recurse option will recursively look at the files imported to find more imports. Will not recurse and look for dependencies within node_modules, but imports to these files are still recorded.

The exclude option is an array of file globs for files you don't want to check when recursing. As with node_modules, imports to these files are still reported.

The parser and parserOptions options are identical to ESLint. By default, it will supports ES6 modules and JSX.

The resolveOptions is passed to resolve to resolvev imports.

Return values

dependencies is a map of absolute file path to an array of imports. Named imports are left as-is, default imports are reported as 'default', and namespace imports are reported as '*'.

I.e.

import { a } from './a'; // 'a'
import { a as b } from './a'; // 'a'
import a from './a'; // 'default'
import * as a from './a'; // '*'

loadedFiles is the files that were loaded and checked.

The default parser and default parser options are exported for your convenience.

Tips

To use babel-eslint, use,

getEsImports({
  ...
  parser: 'babel-eslint',
  ...
})

Keywords

FAQs

Package last updated on 07 May 2016

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