{"id":10611,"library":"cardinal","title":"Cardinal JavaScript Syntax Highlighter","description":"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.","status":"abandoned","version":"2.1.1","language":"javascript","source_language":"en","source_url":"git://github.com/thlorenz/cardinal","tags":["javascript","syntax","highlight","theme","json","terminal","console","print"],"install":[{"cmd":"npm install cardinal","lang":"bash","label":"npm"},{"cmd":"yarn add cardinal","lang":"bash","label":"yarn"},{"cmd":"pnpm add cardinal","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"import { highlight } from 'cardinal';","symbol":"highlight","correct":"const cardinal = require('cardinal');\nconst highlightedCode = cardinal.highlight(codeString);"},{"note":"The primary export is the module itself, containing the `highlightFileSync` method. Importing named exports like `highlightFileSync` directly is not supported.","wrong":"import cardinal from 'cardinal';\nconst highlightedFile = cardinal.highlightFileSync('./myFile.js');","symbol":"highlightFileSync","correct":"const cardinal = require('cardinal');\nconst highlightedFile = cardinal.highlightFileSync('./myFile.js');"},{"note":"This function provides an asynchronous way to highlight file content, using a Node.js-style callback pattern, consistent with its CommonJS design.","wrong":"import { highlightFile } from 'cardinal';","symbol":"highlightFile","correct":"const cardinal = require('cardinal');\ncardinal.highlightFile('./myFile.js', (err, highlighted) => {\n  if (err) console.error(err);\n  else console.log(highlighted);\n});"}],"quickstart":{"code":"const cardinal = require('cardinal');\nconst fs = require('fs');\nconst path = require('path');\n\nconst codeToHighlight = `\nfunction greet(name) {\n  const message = 'Hello, ' + name + '!';\n  // A comment about the greeting\n  console.log(message);\n}\n\ngreet('World');\n`;\n\nconst filePath = path.join(__dirname, 'example.js');\nfs.writeFileSync(filePath, codeToHighlight);\n\nconsole.log('--- Synchronous String Highlight ---');\ntry {\n  const highlightedString = cardinal.highlight(codeToHighlight, { linenos: true });\n  console.log(highlightedString);\n} catch (e) {\n  console.error('Error highlighting string:', e.message);\n}\n\nconsole.log('\\n--- Synchronous File Highlight ---');\ntry {\n  const highlightedFile = cardinal.highlightFileSync(filePath, { theme: 'github', linenos: false });\n  console.log(highlightedFile);\n} catch (e) {\n  console.error('Error highlighting file sync:', e.message);\n}\n\nconsole.log('\\n--- Asynchronous File Highlight ---');\ncardinal.highlightFile(filePath, { jsx: true }, (err, highlighted) => {\n  if (err) {\n    console.error('Error highlighting file async:', err.message);\n  } else {\n    console.log(highlighted);\n  }\n  fs.unlinkSync(filePath); // Clean up temp file\n});","lang":"javascript","description":"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."},"warnings":[{"fix":"Remove the `json: true` option from your `opts` object. Cardinal will correctly process JSON input without it.","message":"The `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.","severity":"gotcha","affected_versions":">=2.x"},{"fix":"When highlighting code that includes JSX, pass `{ jsx: true }` in the options parameter to `highlight`, `highlightFileSync`, or `highlightFile`.","message":"JSX 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.","severity":"gotcha","affected_versions":">=2.x"},{"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.","message":"When 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.","severity":"gotcha","affected_versions":">=2.x"},{"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.","message":"The 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.","severity":"breaking","affected_versions":">=2.1.1"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","cause":"The input string or file contains invalid JavaScript syntax that cardinal's parser cannot understand, or JSX without the `jsx: true` option.","error":"Error: Cannot parse code"},{"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.","cause":"Attempting to use `require('cardinal')` in an ES Module context, where `require` is not natively available.","error":"TypeError: require is not a function"}],"ecosystem":"npm"}