Recursive AI

Extract JavaScript imports and exports in C#

OneEcma is a free, open-source JavaScript interpreter for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Read static imports and exports without running the module.

Terminal
dotnet add package RecursiveAI.OneEcma

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneEcma.Syntax;
var goal = new ParseOptions {
    Goal = ParseGoal.Module };
var tree = JsParser.Parse("import 'x';", goal);
if (tree.HasErrors) return;
foreach (var node in tree.Program.Body)
    if (node is ImportDeclaration item)
        Console.WriteLine(item.Source.Value);
Text
x

Unzip the example, then dotnet run.

The full program

The full example inspects this inline module. The referenced files do not need to exist because this program only parses declarations.

C#
using RecursiveAI.OneEcma.Syntax;
 
string source = """
import main, { value as local } from './data.js';
import './setup.js';
export { local as value };
export { other } from './other.js';
export * from './shared.js';
export default main;
""";
var result = JsParser.Parse(source,
    new ParseOptions { Goal = ParseGoal.Module, FileName = "entry.mjs" });
if (result.HasErrors)
{
    foreach (var error in result.Diagnostics)
        Console.WriteLine(error.Headline);
    return;
}
// Static module declarations are top-level; no recursive expression walk is needed.
foreach (var node in result.Program.Body)
{
    string? dependency = node switch
    {
        ImportDeclaration import => import.Source.Value,
        ExportNamedDeclaration export => export.Source?.Value,
        ExportAllDeclaration export => export.Source.Value,
        _ => null
    };
    if (node is ImportDeclaration or ExportNamedDeclaration
        or ExportAllDeclaration or ExportDefaultDeclaration)
        Console.WriteLine($"{node.Kind} [{node.Start},{node.End})" +
            (dependency is null ? "" : $" -> {dependency}"));
}
Text
ImportDeclaration [0,49) -> ./data.js
ImportDeclaration [50,70) -> ./setup.js
ExportNamedDeclaration [71,97)
ExportNamedDeclaration [98,133) -> ./other.js
ExportAllDeclaration [134,162) -> ./shared.js
ExportDefaultDeclaration [163,183)

If parsing reports errors, the program prints the diagnostics and stops before extracting declarations.

Variations

Read local import names. An imported name can have a different local alias:

C#
using RecursiveAI.OneEcma.Syntax;
 
string source = "import { value as local } from './data.js';";
var result = JsParser.Parse(source,
    new ParseOptions { Goal = ParseGoal.Module });
if (result.HasErrors) throw new Exception("Invalid module");
foreach (var node in result.Program.Body)
    if (node is ImportDeclaration import)
        foreach (var specifier in import.Specifiers)
            Console.WriteLine(specifier.Local.Name);
Text
local

The short answer

Parse with JsParser.Parse and ParseGoal.Module, reject results with HasErrors, then inspect Program.Body. ImportDeclaration.Source.Value is the module specifier. Re-export dependencies appear on ExportNamedDeclaration.Source and ExportAllDeclaration.Source. This extracts source-level dependencies in C# without resolving module paths or executing JavaScript.

An export list from the current file is not another file to load.

How do I parse JavaScript imports and exports in C#?

Use the module goal explicitly. A classic script is the default parse goal, and it is not the right grammar for static import/export declarations. Once parsing succeeds, scan the top-level body in source order.

The output distinguishes ordinary imports, named exports, export-all declarations and the default export. A side-effect-only import has an empty specifier list but still has a dependency. A named export with a from clause also has a dependency, even though the declaration starts with export rather than import.

Use Start and End to select the original declaration text. They are UTF-16 offsets, with the end excluded. This matches C# string indexing, not UTF-8 byte offsets. Convert with SourceText.GetPosition when you need a line and column. Do not modify the source and then apply old offsets to the new text.

What are the different types of imports?

MDN’s import reference distinguishes named, default, namespace and side-effect imports. The parser represents all of those static forms as ImportDeclaration nodes. Inspect Specifiers for the introduced bindings; the variation shows the alias local, rather than the exported name value.

Keep two inventories if your tool needs both dependencies and bindings. Dependency extraction records where declarations point. Binding extraction records names made available in this module. A side-effect import contributes to the first inventory but not the second. Re-exports likewise need not create local import bindings.

What is dynamic import in JavaScript?

Dynamic import(...) is an expression, not a static import declaration. OneEcma represents it as ImportCall, while import.meta is a MetaProperty. The top-level declaration scan here intentionally does not claim to find every dynamic dependency. Such expressions can occur inside functions and other expressions, and their argument can be computed at runtime.

For dynamic-import inspection, a consumer must walk expression nodes as well as declarations, using an iterative traversal or a depth counter. Even finding an ImportCall does not establish a concrete file path when its argument depends on runtime values. Do not label this static inventory a complete runtime module graph.

Module specifiers are strings, not already-resolved paths. The parser does not search package directories, apply application import maps or check another file’s exports. Those are loading and linking decisions. For that separate task, use the module execution recipe; keep parsing alone when you only need to inspect source.

All oneecma recipes