ECMAScript/TypeScript Import and Export Parser

0.2.4 · active · verified Tue Apr 21

parse-imports-exports is a JavaScript/TypeScript library designed for fast and easy parsing of ECMAScript and TypeScript import and export declarations. It currently stands at version 0.2.4, indicating it is in active development and pre-1.0, so the API might evolve. The library focuses on extracting structured information about module dependencies from syntactically correct and well-formatted code, supporting both standard ES module syntax and TypeScript-specific constructs like `import type` and `export type`. It is optimized for efficiency and is not a full Abstract Syntax Tree (AST) parser, but rather targets specific module-related statements, including dynamic `import()` and `require()` calls.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use `parseImportsExports` to analyze a TypeScript/ECMAScript source string and extract a detailed breakdown of all import, export, and re-export declarations, including dynamic imports and CommonJS `require` statements.

import { parseImportsExports } from 'parse-imports-exports';

const source = `
/**
 * Imports.
 */
import {foo as baz, type Bar} from 'Qux';
import Foo, * as foo from 'Qux';
const dynamicQux = await import('Qux');
const commonJSQux = require('Qux');
import type {Foo as Baz, Bar} from 'Qux';

/**
 * Reexports.
 */
export {foo as baz, type Bar} from 'Qux';
export * as foo from 'Qux';
export * from 'Qux';

/**
 * Exports.
 */
export default 42;
export const myVar = 2;
export type T = number;
`;

const importsExports = parseImportsExports(source);

console.log(JSON.stringify(importsExports, null, 2));
/*
Example output (indices may vary):
{
  "namedImports": {
    "Qux": [
      {
        "start": 42,
        "end": 83,
        "names": {
          "baz": {
            "by": "foo"
          }
        },
        "types": {
          "Bar": {}
        }
      }
    ]
  },
  "namespaceImports": {
    "Qux": [
      {
        "start": 85,
        "end": 117,
        "namespace": "foo",
        "default": "Foo"
      }
    ]
  },
  "dynamicImports": {
    "Qux": [
      {
        "start": 129,
        "end": 165
      }
    ]
  },
  "requires": {
    "Qux": [
      {
        "start": 177,
        "end": 206
      }
    ]
  },
  "typeNamedImports": {
    "Qux": [
      {
        "start": 218,
        "end": 251,
        "names": {
          "Baz": {
            "by": "Foo"
          }
        },
        "types": {
          "Bar": {}
        }
      }
    ]
  },
  "namedReexports": {
    "Qux": [
      {
        "start": 279,
        "end": 320,
        "names": {
          "baz": {
            "by": "foo"
          }
        },
        "types": {
          "Bar": {}
        }
      }
    ]
  },
  "namespaceReexports": {
    "Qux": [
      {
        "start": 322,
        "end": 349,
        "namespace": "foo"
      }
    ]
  },
  "wildcardReexports": {
    "Qux": [
      {
        "start": 351,
        "end": 371
      }
    ]
  },
  "defaultExports": [
    {
      "start": 399,
      "end": 415
    }
  ],
  "namedExports": [
    {
      "start": 427,
      "end": 443,
      "names": {
        "myVar": {}
      }
    }
  ],
  "typeExports": [
    {
      "start": 455,
      "end": 471,
      "names": {
        "T": {}
      }
    }
  ]
}
*/

view raw JSON →