Registry / devops / monocart-formatter

monocart-formatter

JSON →
library3.0.1jsnpmunverified

A JS/CSS/HTML/JSON formatter based on js-beautify, version 3.0.1. It provides formatted output with a mapping index to track changes between original and formatted positions. Supports Web Worker in browsers and Worker Threads in Node.js. Ships TypeScript types. Release cadence is irregular; key differentiator is the content mapping feature for source-to-formatted position tracking.

npm install monocart-formatter
INSTALL
IMPORT
SIG · MONOCART-FORMATTER
M
monocart-formatter
devopsjavascriptv3.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

format
import { format } from 'monocart-formatter'
const { format } = require('monocart-formatter')
Package is ESM-only, use import syntax. CommonJS require will fail.
MappingParser
import { MappingParser } from 'monocart-formatter'
import MappingParser from 'monocart-formatter'
MappingParser is a named export, not default. Default export does not exist.
formatSync
import { formatSync } from 'monocart-formatter'
Available since v3.0.0 for synchronous formatting; requires careful use in main threads.

Formats JS code with indentation and demonstrates mapping from original to formatted position.

import { format, MappingParser } from 'monocart-formatter'; const code = "var a=1;"; const type = 'js'; const options = { indent_size: 2 }; (async () => { const { content, mapping } = await format(code, type, options); console.log('Formatted:', content); // 'var a = 1;' const parser = new MappingParser(mapping); const formattedPos = parser.originalToFormatted(10); console.log('Original pos 10 -> formatted:', formattedPos); })().catch(console.error);
Debug
Known issues
breakingIn v3.0.0, the module switched from CommonJS to ESM. require() calls will throw ERR_REQUIRE_ESM.
fix
Use import syntax instead of require(). If using Node.js <12, upgrade to >=12 or use dynamic import().
affects: >=3.0.0
deprecatedThe 'worker' option in format() is deprecated as of v2.5.0. Workers now auto-detect environment.
fix
Remove the worker option; set useWorker: true/false explicitly if needed.
affects: >=2.5.0 <3.0.0
gotchaThe MappingParser methods originalToFormatted and formattedToOriginal may return -1 for unmapped positions.
fix
Always check for -1 in your code before using the result as an index.
affects: *
Errors
Common errors & fixes
SyntaxError: Cannot use import statement outside a module
Attempting to use import in a CommonJS environment (e.g., Node.js with type: 'commonjs' or no type field).
fix
Add 'type': 'module' to your package.json or rename file to .mjs.
TypeError: format is not a function
Default import used instead of named import (import format from 'monocart-formatter' ).
fix
Use named import: import { format } from 'monocart-formatter'.
Error: Cannot find module 'js-beautify'
Missing peer dependency js-beautify. Package expects it installed.
fix
Install js-beautify: npm install js-beautify
Upgrade
Version history
3.0.1latest on npm
Audit
Dependencies
js-beautifyrequiredCore formatting engine for JS/CSS/HTML
Agent activity
2 hits · last 30 days
node
2
Resources
monocart-formatter — npm install monocart-formatter · libregistry