Registry / web-framework / edge-parser

edge-parser

JSON →
library9.1.0jsnpmunverified

edge-parser is a JavaScript/TypeScript library designed to parse the syntax of the Edge template engine, converting template strings into executable JavaScript functions. It is currently stable at version 9.1.0 (published December 2025), with recent releases focusing on features like update expression support and dependency updates. The library provides a programmatic API for tokenizing template input, transforming its Abstract Syntax Tree (AST), and processing tokens into a final JavaScript output, which can then be invoked with template state. A key differentiator is its explicit handling of error tracing within compiled templates via `filename` and `lineNumber` parameters, and highly configurable options for managing template variables (`statePropertyName`) and helper function paths (`escapeCallPath`, `toAttributesCallPath`).

npm install edge-parser
INSTALL
IMPORT
SIG · EDGE-PARSER
E
edge-parser
web-frameworkjavascriptv9.1.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
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 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

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

Parser
✓ import { Parser } from 'edge-parser'
✗ const Parser = require('edge-parser')
The package became ESM-only in v9.0.0. Use `import` syntax. `Parser` is the main class for parsing Edge templates.
EdgeBuffer
✓ import { EdgeBuffer } from 'edge-parser'
✗ const EdgeBuffer = require('edge-parser').EdgeBuffer
Part of the core toolkit for buffering the generated JavaScript output. ESM-only since v9.0.0.
Stack
✓ import { Stack } from 'edge-parser'
✗ const Stack = require('edge-parser').Stack
A utility class often used internally by the Parser to manage parsing context. ESM-only since v9.0.0.
types
✓ import type { ParserConfig } from 'edge-parser/types'
✗ import type { ParserConfig } from 'edge-parser'
Since v9.0.0, TypeScript types are exported from the `edge-parser/types` subpath, not directly from the main package entrypoint.

This code demonstrates how to initialize the Edge Parser, tokenize a template string, process the tokens into a JavaScript function, and then execute that function with a given state and helper utilities, including basic error handling.

import { Parser, EdgeBuffer, Stack } from 'edge-parser'; const filename = 'eval.edge'; const parser = new Parser({}, new Stack(), { statePropertyName: 'state', escapeCallPath: 'escape', toAttributesCallPath: 'toAttributes', }); const buffer = new EdgeBuffer(filename, { outputVar: 'out', rethrowCallPath: 'reThrow' }); const templateString = 'Hello {{ username ?? 'Guest' }}! Today is {{ new Date().toLocaleDateString() }}.'; parser .tokenize(templateString, { filename }) .forEach((token) => parser.processToken(token, buffer)); const output = buffer.flush(); console.log('--- Compiled JavaScript Output ---'); console.log(output); // To run the compiled template: const fn = new Function('state, escape, reThrow', output); const state = { username: 'Alice' }; const escape = (val) => String(val).replace(/[&<>'"/]/g, c => `&#${c.charCodeAt(0)};`); const reThrow = (err, filename, lineNumber) => { console.error(`Error in ${filename} at line ${lineNumber}:`, err.message); throw err; }; try { const result = fn(state, escape, reThrow); console.log('\n--- Template Render Result ---'); console.log(result); } catch (e) { console.error('\n--- Template Execution Error ---'); console.error(e); }
Debug
Known issues
breakingVersion 9.0.0 of `edge-parser` transitioned to an ESM-only package. Attempting to use `require()` for imports will result in an `ERR_REQUIRE_ESM` error.
fix
Migrate all imports from CommonJS `require()` to ES Modules `import` syntax. Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`).
affects: >=9.0.0
breakingSince version 9.0.0, TypeScript type definitions are no longer exported directly from the main `edge-parser` package entrypoint. They are now located under a subpath.
fix
Update type imports to use the `edge-parser/types` subpath, e.g., `import type { ParserConfig } from 'edge-parser/types';`
affects: >=9.0.0
gotchaThe `filename` option passed to `Parser.tokenize` and `EdgeBuffer` is crucial for generating meaningful stack traces in case of template execution errors. Without it, debugging runtime issues in compiled templates becomes significantly harder.
fix
Always provide a descriptive `filename` string when calling `parser.tokenize(template, { filename })` and initializing `new EdgeBuffer(filename, ...)`. This `filename` is embedded in the compiled output for error reporting.
affects: >=1.0.0
gotchaThe `Parser` and `EdgeBuffer` constructors require specific configuration objects (`statePropertyName`, `escapeCallPath`, `toAttributesCallPath`, `outputVar`, `rethrowCallPath`). Incorrectly configured paths or variable names will lead to runtime errors in the generated template function.
fix
Carefully review the documentation for each configuration option and ensure the provided values correctly map to the expected global variables or helper functions available at the time the compiled template function is executed. For example, `escapeCallPath: 'escape'` implies an `escape` function will be available in the template's execution scope.
affects: >=1.0.0
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\path\to\node_modules\edge-parser\dist\index.js from C:\path\to\your_file.js not supported.
Attempting to import `edge-parser` using CommonJS `require()` syntax in a Node.js environment after the package transitioned to ESM-only in v9.0.0.
fix
Change `const { Parser } = require('edge-parser');` to `import { Parser } from 'edge-parser';`. Ensure your `package.json` has `"type": "module"` or use a `.mjs` file extension for your script.
TypeError: Parser is not a constructor
This error typically occurs if `edge-parser` is imported incorrectly (e.g., using `require()` in an ESM context, or attempting a default import when only named exports exist, or mismatching ESM/CJS in older Node versions).
fix
For versions `>=9.0.0`, ensure you use `import { Parser } from 'edge-parser';`. For versions `<9.0.0`, ensure you are correctly importing named exports (if using `require`, it might be `const { Parser } = require('edge-parser');` or `const Parser = require('edge-parser').Parser;` depending on exact package structure).
Cannot find module 'edge-parser/types'
Attempting to import types from `edge-parser/types` in a version older than v9.0.0, or a typo in the import path.
fix
If using `edge-parser@<9.0.0`, types were likely directly from `edge-parser`. Update your import to `import type { ParserConfig } from 'edge-parser';`. If on `edge-parser@>=9.0.0`, double-check the exact subpath spelling: `import type { ParserConfig } from 'edge-parser/types';`
ReferenceError: state is not defined (when executing the compiled template function)
The compiled template function expects a `state` variable (or whatever `statePropertyName` is configured to) in its scope, but it was not provided or was named differently during function invocation.
fix
When creating `new Parser()`, ensure `statePropertyName` matches the variable name you intend to pass to the compiled function. When invoking the `new Function(...)` output, pass the state object as the first argument, e.g., `fn(myState, escape, reThrow)`.
Upgrade
Version history
9.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
10
Resources