ECMAScript/TypeScript Import and Export Parser
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
-
SyntaxError: Unexpected token
cause The input source string contains invalid JavaScript/TypeScript syntax or is poorly formatted, which causes the underlying parser to fail.fixValidate and format your source code using a tool like ESLint or Prettier before passing it to `parseImportsExports`. Ensure all import/export statements are syntactically correct. -
TypeError: parseImportsExports is not a function
cause This typically occurs when trying to use CommonJS `require` to import the module or attempting to destructure a non-existent export, especially in an ESM-first project.fixUse `import { parseImportsExports } from 'parse-imports-exports';` in modern Node.js and browser environments that support ES modules. If using CommonJS, ensure your project setup correctly transpiles or resolves ESM imports. -
Could not parse import/exports with acorn: $error
cause This error message indicates an issue with the underlying parser (Acorn, or similar) failing to interpret a specific import or export statement due to invalid syntax or an unexpected token.fixReview the specific import/export statement causing the error. Ensure it adheres strictly to ECMAScript/TypeScript syntax, including correct use of keywords, module specifiers, and proper termination.
Warnings
- gotcha This package requires input code to be syntactically correct and well-formatted. It is not designed to validate or lint code with errors, but to parse valid module declarations.
- breaking As the package is in an early `0.x.x` version series, its API (including the structure of the returned `ImportExportResult` object) is subject to non-backward-compatible changes in minor releases.
- gotcha The parser works specifically for `import` and `export` declarations and `require()` calls. It does not provide a full Abstract Syntax Tree (AST) for the entire file, nor does it handle all possible JavaScript/TypeScript syntax errors beyond its scope.
Install
-
npm install parse-imports-exports -
yarn add parse-imports-exports -
pnpm add parse-imports-exports
Imports
- parseImportsExports
const parseImportsExports = require('parse-imports-exports');import { parseImportsExports } from 'parse-imports-exports'; - ImportExportResult
import { ImportExportResult } from 'parse-imports-exports';import type { ImportExportResult } from 'parse-imports-exports'; - All symbols at once
import { parseImportsExports, ImportExportResult } from 'parse-imports-exports';
Quickstart
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": {}
}
}
]
}
*/