Registry / devops / cli-source-preview

cli-source-preview

JSON →
library1.1.0jsnpmunverified

cli-source-preview is a JavaScript utility designed to render syntax-highlighted source code snippets directly in the command-line interface. It's particularly useful for enhancing error messages or debug outputs by providing relevant code context, complete with line numbers and customizable colorization. The current stable version is 1.1.0. This package offers a focused solution for presenting code in a terminal environment, distinguishing itself by integrating `chalk` for robust ASCII color support. Developers can specify single lines, line ranges, or even precise line/column positions for the preview, with options to control the number of surrounding context lines and the line delimiter. Given its specific utility and the current version, it's maintained to ensure compatibility and address any critical bugs, rather than undergoing frequent feature-driven releases.

npm install cli-source-preview
INSTALL
IMPORT
SIG · CLI-SOURCE-PREVIEW
C
cli-source-preview
devopsjavascriptv1.1.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.

preview
const preview = require('cli-source-preview')
import preview from 'cli-source-preview'
The `cli-source-preview` package (v1.x) is CommonJS-only and does not export ES Modules. Use `require`.
preview.readSource
const preview = require('cli-source-preview'); const readSource = preview.readSource;
import { readSource } from 'cli-source-preview'
`readSource` is a method on the default `preview` export, not a named export. It is also CommonJS-only.

This quickstart demonstrates how to install and use `cli-source-preview` to highlight and preview different sections of a source code string, including single lines, line ranges, and specific line/column positions.

const preview = require('cli-source-preview'); const source = ` 'use strict' const chalk = require('chalk') const PREVIEW_OPTS = { offset: 5, lineNumber: true, delimiter: '\n' } const DELIMITER = '-'.repeat(40) function rightPad (text, width) { return text + (width > text.length ? ' '.repeat(width - text.length) : '') } `; // Preview line 10 console.log('--- Preview line 10 ---'); console.log(preview(source, 10)); // Preview lines 5 to 8 console.log('\n--- Preview lines 5-8 ---'); console.log(preview(source, [5, 8])); // Preview line 12, column 6 console.log('\n--- Preview line 12:6 ---'); console.log(preview(source, { line: 12, column: 6}));
Debug
Known issues
breakingThe package `cli-source-preview` v1.x is strictly CommonJS (CJS) and does not provide ES Module (ESM) exports. Attempting to `import` it in an ESM context will result in a runtime error.
fix
Use `const preview = require('cli-source-preview');` in CJS files. For ESM projects, consider using a CJS-compatible wrapper or a bundler that can handle CJS modules.
affects: >=1.0.0
gotchaThe package depends on `chalk` for its CLI coloring. Ensure `chalk` is correctly installed in your project. Mismatched or missing `chalk` versions might lead to unexpected styling or runtime errors if `cli-source-preview`'s internal `chalk` dependency cannot be resolved.
gotchaThe `line` argument for the `preview` function is `Mixed` and can accept a number, an array `[start, end]`, or an object `{ line, column }`. Providing incorrect types or out-of-bounds line/column numbers might lead to unexpected output or errors, though the library generally handles basic out-of-bounds gracefully by adjusting the preview range.
fix
Always validate input line numbers and ranges against the `source` string's actual line count to prevent unexpected behavior.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined in ES module scope
Attempting to use `require('cli-source-preview')` inside a JavaScript file that Node.js is treating as an ES Module (e.g., due to `"type": "module"` in `package.json` or a `.mjs` extension).
fix
Rename your file to a `.cjs` extension, or ensure your `package.json` specifies `"type": "commonjs"`. Alternatively, use dynamic `import()` if you need to load a CJS module from an ESM context: `const preview = await import('cli-source-preview');` (note: this still imports the CJS default, which is the `preview` function itself, so `preview.default` might be needed if not destructured).
Error: Cannot find module 'cli-source-preview'
The `cli-source-preview` package has not been installed or cannot be resolved by the Node.js module loader.
fix
Run `npm install cli-source-preview` in your project directory. If it's installed globally, ensure your Node.js `require` path includes global modules, or install it locally.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies
chalkrequiredProvides ASCII CLI highlight colors for the source code preview.
Agent activity
2 hits · last 30 days
node
2
Resources
cli-source-preview — npm install cli-source-preview · libregistry