{"id":16166,"library":"parse-imports-exports","title":"ECMAScript/TypeScript Import and Export Parser","description":"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.","status":"active","version":"0.2.4","language":"javascript","source_language":"en","source_url":"https://github.com/joomcode/parse-imports-exports","tags":["javascript","esm","export","import","parse","parser","typescript"],"install":[{"cmd":"npm install parse-imports-exports","lang":"bash","label":"npm"},{"cmd":"yarn add parse-imports-exports","lang":"bash","label":"yarn"},{"cmd":"pnpm add parse-imports-exports","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"While the library can parse CommonJS `require` statements, it is itself an ESM-first package shipping TypeScript types, so ESM `import` is the idiomatic usage.","wrong":"const parseImportsExports = require('parse-imports-exports');","symbol":"parseImportsExports","correct":"import { parseImportsExports } from 'parse-imports-exports';"},{"note":"This is a type definition for the parsed output structure. Use `import type` to avoid bundling unnecessary runtime code in environments that support it.","wrong":"import { ImportExportResult } from 'parse-imports-exports';","symbol":"ImportExportResult","correct":"import type { ImportExportResult } from 'parse-imports-exports';"},{"note":"Multiple named exports can be imported in a single statement.","symbol":"All symbols at once","correct":"import { parseImportsExports, ImportExportResult } from 'parse-imports-exports';"}],"quickstart":{"code":"import { parseImportsExports } from 'parse-imports-exports';\n\nconst source = `\n/**\n * Imports.\n */\nimport {foo as baz, type Bar} from 'Qux';\nimport Foo, * as foo from 'Qux';\nconst dynamicQux = await import('Qux');\nconst commonJSQux = require('Qux');\nimport type {Foo as Baz, Bar} from 'Qux';\n\n/**\n * Reexports.\n */\nexport {foo as baz, type Bar} from 'Qux';\nexport * as foo from 'Qux';\nexport * from 'Qux';\n\n/**\n * Exports.\n */\nexport default 42;\nexport const myVar = 2;\nexport type T = number;\n`;\n\nconst importsExports = parseImportsExports(source);\n\nconsole.log(JSON.stringify(importsExports, null, 2));\n/*\nExample output (indices may vary):\n{\n  \"namedImports\": {\n    \"Qux\": [\n      {\n        \"start\": 42,\n        \"end\": 83,\n        \"names\": {\n          \"baz\": {\n            \"by\": \"foo\"\n          }\n        },\n        \"types\": {\n          \"Bar\": {}\n        }\n      }\n    ]\n  },\n  \"namespaceImports\": {\n    \"Qux\": [\n      {\n        \"start\": 85,\n        \"end\": 117,\n        \"namespace\": \"foo\",\n        \"default\": \"Foo\"\n      }\n    ]\n  },\n  \"dynamicImports\": {\n    \"Qux\": [\n      {\n        \"start\": 129,\n        \"end\": 165\n      }\n    ]\n  },\n  \"requires\": {\n    \"Qux\": [\n      {\n        \"start\": 177,\n        \"end\": 206\n      }\n    ]\n  },\n  \"typeNamedImports\": {\n    \"Qux\": [\n      {\n        \"start\": 218,\n        \"end\": 251,\n        \"names\": {\n          \"Baz\": {\n            \"by\": \"Foo\"\n          }\n        },\n        \"types\": {\n          \"Bar\": {}\n        }\n      }\n    ]\n  },\n  \"namedReexports\": {\n    \"Qux\": [\n      {\n        \"start\": 279,\n        \"end\": 320,\n        \"names\": {\n          \"baz\": {\n            \"by\": \"foo\"\n          }\n        },\n        \"types\": {\n          \"Bar\": {}\n        }\n      }\n    ]\n  },\n  \"namespaceReexports\": {\n    \"Qux\": [\n      {\n        \"start\": 322,\n        \"end\": 349,\n        \"namespace\": \"foo\"\n      }\n    ]\n  },\n  \"wildcardReexports\": {\n    \"Qux\": [\n      {\n        \"start\": 351,\n        \"end\": 371\n      }\n    ]\n  },\n  \"defaultExports\": [\n    {\n      \"start\": 399,\n      \"end\": 415\n    }\n  ],\n  \"namedExports\": [\n    {\n      \"start\": 427,\n      \"end\": 443,\n      \"names\": {\n        \"myVar\": {}\n      }\n    }\n  ],\n  \"typeExports\": [\n    {\n      \"start\": 455,\n      \"end\": 471,\n      \"names\": {\n        \"T\": {}\n      }\n    }\n  ]\n}\n*/","lang":"typescript","description":"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."},"warnings":[{"fix":"Ensure the source string passed to `parseImportsExports` is valid JavaScript/TypeScript, potentially pre-processed by a linter or formatter like Prettier.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Refer to the official GitHub repository for the latest API documentation and breaking change notes before upgrading. Consider pinning to exact minor versions to avoid unexpected changes.","message":"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.","severity":"breaking","affected_versions":">=0.1.0"},{"fix":"Use a dedicated full AST parser (e.g., Acorn, Babel, TypeScript compiler API) if you require comprehensive code analysis or error detection beyond module declarations.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Validate 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.","cause":"The input source string contains invalid JavaScript/TypeScript syntax or is poorly formatted, which causes the underlying parser to fail.","error":"SyntaxError: Unexpected token"},{"fix":"Use `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.","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.","error":"TypeError: parseImportsExports is not a function"},{"fix":"Review 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.","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.","error":"Could not parse import/exports with acorn: $error"}],"ecosystem":"npm"}