Registry / type-stubs / rs-module-lexer

rs-module-lexer

JSON →
library2.8.0jsnpmunverified

rs-module-lexer is an ES module parser built with Rust, offering a drop-in compatible output data structure with `es-module-lexer`. Currently at version 2.8.0, it maintains an active release cadence with frequent minor updates. Its key differentiator is the ability to directly parse TypeScript (TS/TSX) and JSX files, unlike `es-module-lexer` which necessitates a prior transformation step. Powered by SWC, this library collapses the transform and parse steps into a single API call, streamlining module analysis in bundlers and build tools. It's designed for scenarios requiring fast and accurate identification of imports and exports across various JavaScript and TypeScript syntaxes, providing a consistent API and data format.

npm install rs-module-lexer
INSTALL
IMPORT
SIG · RS-MODULE-LEXER
R
rs-module-lexer
type-stubsjavascriptv2.8.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parseAsync
import { parseAsync } from 'rs-module-lexer'
const { parseAsync } = require('rs-module-lexer')
The primary asynchronous API for parsing, recommended for most use cases, especially in Node.js environments. Since v2.1.1.
parse
import { parse } from 'rs-module-lexer'
const { parse } = require('rs-module-lexer')
The synchronous API for parsing. Use `parseAsync` when possible to avoid blocking the event loop.
ModuleInfo
import type { ModuleInfo } from 'rs-module-lexer'
TypeScript type for the output structure of a parsed module, aligning with es-module-lexer's data shape.

Demonstrates parsing multiple TypeScript and JavaScript files asynchronously, showcasing import and export extraction.

import { parseAsync } from 'rs-module-lexer'; async function analyzeCode() { const tsCode = ` import { useState, useEffect } from 'react'; import { someUtil } from './utils'; export const MyComponent = () => { const [count, setCount] = useState(0); useEffect(() => { console.log('Count:', count); }, [count]); return <div>Hello</div>; }; export function helper() { /* ... */ } `; const { output } = await parseAsync({ input: [ { filename: 'my-component.tsx', code: tsCode, }, { filename: 'another-file.js', code: 'import { foo } from "bar";', } ], }); // Accessing the results for 'my-component.tsx' const myComponentResult = output[0]; console.log('Imports for my-component.tsx:', myComponentResult.imports); console.log('Exports for my-component.tsx:', myComponentResult.exports); // Accessing the results for 'another-file.js' const anotherFileResult = output[1]; console.log('Imports for another-file.js:', anotherFileResult.imports); } analyzeCode().catch(console.error);
Debug
Known issues
breakingWhen migrating from `es-module-lexer`, the API signature changes from a direct return of `[imports, exports, facade, hasModuleSyntax]` to an object `{ output }` where `output` is an array of results, each containing these properties. You must destructure `output[0]` (or the relevant index) to access the parsing results for a single file.
fix
Change `const [imports, exports] = parse(code)` to `const { output } = await parseAsync({ input: [{ filename, code }] }); const { imports, exports } = output[0];`
affects: >=2.0.0
gotchaThe `parseAsync` method expects an array of input objects, each with a `filename` and `code` property. The `filename` extension (`.ts`, `.tsx`, `.js`, `.jsx`) is crucial for `rs-module-lexer` to correctly auto-detect the syntax and parse the file.
fix
Always provide a `filename` with the correct extension in the input object: `parseAsync({ input: [{ filename: 'component.tsx', code: '...' }] })`
affects: >=2.0.0
breakingVersion 2.0.0 introduced support for the 'import with' syntax (e.g., `import json from 'source' with { type: 'json' }`) and export object destructing. While aligning with `es-module-lexer` and modern ES module syntax, this might alter parsing results for code leveraging these features compared to `rs-module-lexer` versions prior to 2.0.0.
fix
No direct fix needed, but be aware of parsing behavior changes if upgrading from pre-2.0.0 versions and using these specific syntaxes.
affects: >=2.0.0
gotchaUnlike `es-module-lexer`, `rs-module-lexer` does not require an `init()` call before parsing. Directly import and use `parse` or `parseAsync`.
fix
Remove any `await init` or `init()` calls when migrating from `es-module-lexer` to `rs-module-lexer`.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: parse is not a function
Attempting to use `parse` without destructuring it from the module, or using CommonJS `require` instead of ES module `import`.
fix
Ensure you are using `import { parse, parseAsync } from 'rs-module-lexer'` for ES modules, or verify your environment supports top-level await for dynamic imports.
Property 'output' does not exist on type '{ imports: Import[]; exports: Export[]; facade: boolean; hasModuleSyntax: boolean; }[]'
Incorrectly assuming the `parseAsync` return value is directly the `imports`/`exports` array, or not accessing the individual file results from the `output` array.
fix
The result of `parseAsync` is an object `{ output: [...] }`. You need to access `result.output[index]` to get the parsing details for a specific file. For a single input file, use `const { output: [firstFileResult] } = await parseAsync(...)`.
Parsing error or incorrect import/export detection for TS/TSX/JSX files.
The `filename` property in the input object was missing or had an incorrect file extension, preventing `rs-module-lexer` from correctly detecting the syntax.
fix
Always provide a `filename` with the accurate extension (e.g., `.ts`, `.tsx`, `.jsx`) in the input object passed to `parseAsync` or `parse`.
Upgrade
Version history
2.8.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
28 hits · last 30 days
node
24
Amazon
1
OpenAI (training)
1
Resources
rs-module-lexer — npm install rs-module-lexer · libregistry