Simple Code Frame
Simple Code Frame is a lightweight JavaScript utility designed for generating structured code frames, primarily used in error reporting or diagnostic tools. Its current stable version is 1.3.0. Unlike more comprehensive alternatives like `@babel/code-frame`, `simple-code-frame` distinguishes itself by operating directly on raw string input without requiring an AST parser. This focused design makes it exceptionally small and performant for tasks that only involve highlighting specific lines and columns within arbitrary text. The library ships with TypeScript types, providing a good developer experience for both JavaScript and TypeScript projects. Releases appear to follow a moderate cadence, adding features like `maxWidth` for line truncation and colored output in recent minor versions. It is suitable for environments where parsing overhead is undesirable, focusing solely on visual presentation of code context.
Common errors
-
TypeError: code.split is not a function
cause The `code` argument passed to `createCodeFrame` was not a string.fixEnsure the first argument passed to `createCodeFrame` is always a string containing the source code. -
TypeError: Cannot read properties of undefined (reading 'split')
cause Similar to `code.split is not a function`, this typically means `code` was `null`, `undefined`, or some other non-string value where the library expected a string.fixVerify that the `code` input parameter is a valid string before calling `createCodeFrame`.
Warnings
- gotcha Line and column numbers for `createCodeFrame` are 1-based, not 0-based. Providing 0 for either will not correctly target the first line/column.
- gotcha When the `line` or `column` argument points to a location outside the bounds of the provided code string, the code frame will still be generated but the marker ('>' and '^') might not appear, leading to unexpected output without an explicit error.
- gotcha Colored output (using `highlightColor`) may not display correctly in terminals that do not support ANSI escape codes, or if the `NO_COLOR` environment variable is set. The library does not force colors.
- gotcha The `maxWidth` option truncates lines with an ellipsis ('...') if they exceed the specified width. This only applies to the highlighted line and its context, not the entire source.
Install
-
npm install simple-code-frame -
yarn add simple-code-frame -
pnpm add simple-code-frame
Imports
- createCodeFrame
import createCodeFrame from 'simple-code-frame';
import { createCodeFrame } from 'simple-code-frame'; - createCodeFrame (CJS)
const createCodeFrame = require('simple-code-frame');const { createCodeFrame } = require('simple-code-frame'); - Types
import type { CodeFrameOptions } from 'simple-code-frame';
Quickstart
import { createCodeFrame } from 'simple-code-frame';
const sourceCode = `function greet(name) {
console.log('Hello, ' + name + '!');
throw new Error('Something went wrong');
}
greet('World');`;
// Simulate an error at line 3, column 9
const errorFrame = createCodeFrame(sourceCode, 3, 9, {
highlightColor: 'red',
message: 'Error here!'
});
console.log(errorFrame);
// Example with maxWidth and no message
const longLineCode = `const veryLongVariableName = 'This is a very long string that should be truncated by maxWidth.';
console.log(veryLongVariableName);`;
const truncatedFrame = createCodeFrame(longLineCode, 1, 1, { maxWidth: 50 });
console.log('\n--- Truncated Frame ---\n');
console.log(truncatedFrame);