{"id":16968,"library":"cli-source-preview","title":"CLI Source Code Preview","description":"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.","status":"active","version":"1.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/yibn2008/cli-source-preview","tags":["javascript","source","highlight","preview"],"install":[{"cmd":"npm install cli-source-preview","lang":"bash","label":"npm"},{"cmd":"yarn add cli-source-preview","lang":"bash","label":"yarn"},{"cmd":"pnpm add cli-source-preview","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Provides ASCII CLI highlight colors for the source code preview.","package":"chalk","optional":false}],"imports":[{"note":"The `cli-source-preview` package (v1.x) is CommonJS-only and does not export ES Modules. Use `require`.","wrong":"import preview from 'cli-source-preview'","symbol":"preview","correct":"const preview = require('cli-source-preview')"},{"note":"`readSource` is a method on the default `preview` export, not a named export. It is also CommonJS-only.","wrong":"import { readSource } from 'cli-source-preview'","symbol":"preview.readSource","correct":"const preview = require('cli-source-preview'); const readSource = preview.readSource;"}],"quickstart":{"code":"const preview = require('cli-source-preview');\n\nconst source = `\n'use strict'\n\nconst chalk = require('chalk')\n\nconst PREVIEW_OPTS = {\n  offset: 5,\n  lineNumber: true,\n  delimiter: '\\n'\n}\nconst DELIMITER = '-'.repeat(40)\n\nfunction rightPad (text, width) {\n  return text + (width > text.length ? ' '.repeat(width - text.length) : '')\n}\n`;\n\n// Preview line 10\nconsole.log('--- Preview line 10 ---');\nconsole.log(preview(source, 10));\n\n// Preview lines 5 to 8\nconsole.log('\\n--- Preview lines 5-8 ---');\nconsole.log(preview(source, [5, 8]));\n\n// Preview line 12, column 6\nconsole.log('\\n--- Preview line 12:6 ---');\nconsole.log(preview(source, { line: 12, column: 6}));\n","lang":"javascript","description":"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."},"warnings":[{"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.","message":"The 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.","severity":"breaking","affected_versions":">=1.0.0"},{"message":"The 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.","severity":"gotcha"},{"fix":"Always validate input line numbers and ranges against the `source` string's actual line count to prevent unexpected behavior.","message":"The `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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"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).","cause":"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).","error":"ReferenceError: require is not defined in ES module scope"},{"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.","cause":"The `cli-source-preview` package has not been installed or cannot be resolved by the Node.js module loader.","error":"Error: Cannot find module 'cli-source-preview'"}],"ecosystem":"npm","meta_description":null}