Registry / serialization / cardinal

cardinal

JSON →
library0.0.9jsnpmunverified

Cardinal is a JavaScript library and command-line tool (cdl) designed to syntax highlight JavaScript and JSON code with ANSI colors for terminal output. It offers features such as customizable color themes, optional line number printing, and support for UNIX pipes. The current stable version is 2.1.1, with its last known publication approximately eight years ago, indicating that the package is no longer actively maintained with regular updates or new features. This makes it suitable for projects requiring a stable, lightweight terminal highlighter with specific historical syntax support, but users should be aware of the lack of ongoing development. Its key differentiators include a strong focus on terminal-specific ANSI coloring and dual API/CLI functionality for direct code strings or files.

npm install cardinal
INSTALL
IMPORT
SIG · CARDINAL
C
cardinal
serializationjavascriptv0.0.9
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.

highlight
const cardinal = require('cardinal'); const highlightedCode = cardinal.highlight(codeString);
import { highlight } from 'cardinal';
Cardinal is a CommonJS module. Direct ES Module imports will typically fail without a compatibility layer or bundler due to its age and lack of native ESM support.
highlightFileSync
const cardinal = require('cardinal'); const highlightedFile = cardinal.highlightFileSync('./myFile.js');
import cardinal from 'cardinal'; const highlightedFile = cardinal.highlightFileSync('./myFile.js');
The primary export is the module itself, containing the `highlightFileSync` method. Importing named exports like `highlightFileSync` directly is not supported.
highlightFile
const cardinal = require('cardinal'); cardinal.highlightFile('./myFile.js', (err, highlighted) => { if (err) console.error(err); else console.log(highlighted); });
import { highlightFile } from 'cardinal';
This function provides an asynchronous way to highlight file content, using a Node.js-style callback pattern, consistent with its CommonJS design.

This quickstart demonstrates how to use `cardinal` to highlight both a JavaScript code string and a JavaScript file, using both synchronous and asynchronous API calls. It also shows how to apply options like line numbers and themes, and includes basic error handling for parsing failures.

const cardinal = require('cardinal'); const fs = require('fs'); const path = require('path'); const codeToHighlight = ` function greet(name) { const message = 'Hello, ' + name + '!'; // A comment about the greeting console.log(message); } greet('World'); `; const filePath = path.join(__dirname, 'example.js'); fs.writeFileSync(filePath, codeToHighlight); console.log('--- Synchronous String Highlight ---'); try { const highlightedString = cardinal.highlight(codeToHighlight, { linenos: true }); console.log(highlightedString); } catch (e) { console.error('Error highlighting string:', e.message); } console.log('\n--- Synchronous File Highlight ---'); try { const highlightedFile = cardinal.highlightFileSync(filePath, { theme: 'github', linenos: false }); console.log(highlightedFile); } catch (e) { console.error('Error highlighting file sync:', e.message); } console.log('\n--- Asynchronous File Highlight ---'); cardinal.highlightFile(filePath, { jsx: true }, (err, highlighted) => { if (err) { console.error('Error highlighting file async:', err.message); } else { console.log(highlighted); } fs.unlinkSync(filePath); // Clean up temp file });
cdl --version
Debug
Known issues
gotchaThe `json` option in `opts` is obsoleted. Cardinal now automatically understands both JSON and JavaScript, making this option redundant and its usage potentially ignored or deprecated.
fix
Remove the `json: true` option from your `opts` object. Cardinal will correctly process JSON input without it.
affects: >=2.x
gotchaJSX syntax support requires explicitly setting `jsx: true` in the options object. By default, JSX will not be highlighted, and `cardinal` will throw a parsing error if encountered.
fix
When highlighting code that includes JSX, pass `{ jsx: true }` in the options parameter to `highlight`, `highlightFileSync`, or `highlightFile`.
affects: >=2.x
gotchaWhen using `cardinal` as part of a UNIX pipe, such as `cat file.js | grep console | cdl`, lines that are not parsable as valid JavaScript may be printed without highlighting.
fix
Ensure that the piped input contains only valid JavaScript segments if full highlighting is expected. Invalid lines will gracefully fall back to unhighlighted output.
affects: >=2.x
breakingThe package `cardinal` has not been updated in approximately eight years. This means it is no longer actively maintained, and users should not expect bug fixes, security updates, or compatibility with newer JavaScript language features or Node.js versions beyond its last stable release. Relying on it in new projects might introduce unaddressed vulnerabilities or compatibility issues.
fix
Evaluate alternatives if active maintenance, modern JavaScript syntax support, or security updates are critical for your project. If using, thoroughly test with your specific Node.js version and JavaScript syntax.
affects: >=2.1.1
Errors
Common errors & fixes
Error: Cannot parse code
The input string or file contains invalid JavaScript syntax that cardinal's parser cannot understand, or JSX without the `jsx: true` option.
fix
Review the code for syntax errors. If using JSX, ensure the `{ jsx: true }` option is provided to the `highlight` function. Wrap calls to `highlight` or `highlightFileSync` in a `try...catch` block to handle parsing errors gracefully.
TypeError: require is not a function
Attempting to use `require('cardinal')` in an ES Module context, where `require` is not natively available.
fix
Since `cardinal` is a CommonJS module, it should be used in a CommonJS environment or through a compatible loader/bundler. If you must use it in an ESM project, consider dynamic `import()` or transpilation if your toolchain supports it, or wrap it in a separate CJS file and import that wrapper.
Upgrade
Version history
0.0.9latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources