🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 4 Min Lesezeit
0

Building Pure Frontend Formatter Tools: Why Node.js Libraries Fail in the Browser

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Building a pure-frontend online formatter tool site seems straightforward: import a formatting library → call it in the browser.

In practice, three commonly-used libraries all failed in the browser.







The Problem List



During development, three popular libraries all broke in the browser:




























Library Purpose Browser Behavior
terser JS minification fs is not defined
html-minifier-terser HTML minification fs is not defined

xml-formatter v3.7.0
XML formatting ⚠️ ESM/CJS interop issues





terser: The fs Module Problem






Reproduction






CODE
import { minify } from 'terser'

// Called in a browser component
const result = await minify(code, { compress: true, mangle: true })






Build succeeds. Runtime error:




CODE
Uncaught ReferenceError: fs is not defined
at Object.readSourceMap (terser/dist/bundle.min.js:1:xxxxx)









Root Cause



terser internally calls fs.readFileSync to read source map files. Fine in Node.js — fs doesn't exist in browsers.



Even though terser's package.json declares a browser field for conditional exports, some internal modules still reference fs, and webpack/SWC tree-shaking doesn't fully eliminate them.






Solution: Lightweight Regex Minifier






CODE
function minifyJs(code) {
return code
// Remove single-line comments
.replace(/\/\/.*$/gm, '')
// Remove multi-line comments
.replace(/\/\*[\s\S]*?\*\//g, '')
// Collapse whitespace
.replace(/\s+/g, ' ')
.trim()
// Remove spaces around punctuation
.replace(/\s*([,;{}()\[\]])\s*/g, '$1')
// Remove blank lines
.replace(/\n\s*\n/g, '\n')
}






Advantages:




  • Zero dependencies, pure JS

  • Works in both browser and Node.js

  • Tiny codebase (20 lines)

  • More than sufficient for a tool site's JS minification



Disadvantages:




  • Lower compression ratio than Terser

  • No AST-level optimizations (variable mangling, dead code elimination)



But for "user pastes code in browser to see minified output," regex is more than enough.






html-minifier-terser: Same fs Problem






Problem



Same root cause as terser. html-minifier-terser calls fs internally to read local files for <script src="..."> etc.






Solution






CODE
function minifyHtml(html) {
return html
.replace(/<!--[\s\S]*?-->/g, '') // Remove HTML comments
.replace(/>\s+</g, '><') // Collapse whitespace between tags
.replace(/\s*=\s*/g, '=') // Tighten attribute values
.replace(/ {2,}/g, ' ') // Collapse multiple spaces
.trim()
}









xml-formatter: ESM/CJS Interop Issues






Problem



This library actually supports the browser. But v3.7.0's export structure is messy:




CODE
// xml-formatter v3.7.0 actual exports
export default function formatXml(...) { ... } // default export
export function minify(xml) { ... } // named export






After SWC compilation (CJS wrapper), the default export becomes exports.default. Direct import formatXml from 'xml-formatter' sometimes returns a non-function object.






Debugging






CODE
// ❌ formatXml is undefined at runtime
import formatXml from 'xml-formatter'
import { minify } from 'xml-formatter' // ✅ This works

// ✅ Correct approach
import _formatXml from 'xml-formatter'
const formatXml = _formatXml.default || _formatXml









Final Solution






CODE
// Format with xml-formatter (interop handled)
import _formatXml from 'xml-formatter'
const formatXml = _formatXml.default || _formatXml

// Minify with custom function (xml-formatter's minify is inconsistent across versions)
function minifyXml(xml) {
return xml
.replace(/<!--[\s\S]*?-->/g, '')
.replace(/\s+/g, ' ')
.replace(/>\s+</g, '><')
.trim()
}









Library Selection Checklist



Before choosing a library, run through this:





  1. Does package.json have a browser field? — Yes → browser-adapted


  2. Does the dependency tree include fs, path, net? — Yes → can't use in browser

  3. Does the npm page mention "Browser Support"?


  4. Search GitHub Issues for "browser", "client-side" — Any similar reports?

  5. Test locally with a simple HTML + <script type="module">






Recommended Libraries for Pure Frontend Tools






































Function Library Why
JS/HTML/XML formatting js-beautify Explicit browser build support
CSS minification csso Pure JS, no Node.js deps
JSON formatting JSON.stringify/parse Native API
SQL formatting sql-formatter Browser-compatible
YAML formatting js-yaml Browser-compatible





Core Lesson




Don't judge a library by its name. Judge it by its runtime dependencies.

"js" in the name doesn't mean it runs in browsers. "node" in the name doesn't mean it can't.







Project



utlkit.com — All formatter tools run purely in the browser. The solutions above are running stably in production.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Building Pure Frontend Formatter Tools: Why Node.js Libraries Fail in the Browser

Thematisch verwandte Begriffe: Building, Pure, Frontend, Formatter · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...