my-bad
v1.3.1
Published
Beautiful dev-server error pages: HTML, ANSI and JSON from one sourcemapped report
Readme
my-bad
Beautiful dev-server error pages rendered to HTML, ANSI or JSON and live updated over SSE. ✨
my-bad turns an error into a JSON report with sourcemapped frames, source snippets, causes, a component trace, and request context. You can render the report as a full HTML page, as an overlay on an existing page, or as ANSI text for the terminal.
A live channel keeps open pages up to date. Pages swap in new errors and reload once an error is fixed. The channel also carries warnings, logs, and problems from tools such as type checkers and linters.
The same report in the terminal:
Install
my-bad requires Node.js 22.12 or later.
npm install my-badUse it with Vite
Add the plugin to your Vite config:
import { myBad } from 'my-bad/vite'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [
myBad()
]
})Compile errors now open in an overlay, with frames mapped through the Vite module graph. The overlay closes when the next update succeeds.
Add Vite DevTools
If you use Vite DevTools, myBad() adds a dock to it. You don't need to configure my-bad. Vite DevTools requires Vite 8.3 or later. Install @vitejs/devtools, then enable DevTools in the same config:
import { myBad } from 'my-bad/vite'
import { defineConfig } from 'vite'
export default defineConfig({
devtools: true,
plugins: [
myBad()
]
})The dock lists the open error, the warnings, and every problem set. Errors no longer take over the page while DevTools is running.
Use it in any dev server
In the error handler of your dev server, build a report and render it. In this example, error, req, and res come from your server:
import { createReport, renderAnsi, renderPage, serializeReport } from 'my-bad'
const cwd = process.cwd()
const report = await createReport(error, { cwd })
console.error(renderAnsi(report, { cwd }))
res.statusCode = 500
if (req.headers.accept?.includes('text/html')) {
res.setHeader('content-type', 'text/html')
res.end(renderPage(report, { cwd }))
}
else {
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify(serializeReport(report, { cwd })))
}To keep pages live, add the channel and the badge. The dev server guide walks through each step.
Show problems from other tools
Type checkers, linters, and audits can publish their current diagnostics as a problem set. Every page that renders the badge shows a count for each tool, and opens the problems beside the page:
import { ESLint } from 'eslint'
import { createProblemReport } from 'my-bad'
const results = await new ESLint().lintFiles(['src'])
channel.setProblems('eslint', results.flatMap(result => result.messages.map(message => createProblemReport({
name: 'ESLint',
message: message.message,
code: message.ruleId ?? undefined,
file: result.filePath,
line: message.line,
column: message.column,
kind: message.severity === 2
? 'error'
: 'warning',
}))), { label: 'ESLint' })Next steps
- Integrate with a dev server: connect reports, the channel, and the badge in your own server.
- Use with Vite: render server errors, and configure the plugin and Vite DevTools.
- Publish problems: show diagnostics from a type checker, linter, or audit.
- Reference: reports, rendering, the live channel, and every export.
💻 Development
git clone [email protected]:danielroe/my-bad.git
corepack enable
# run interactive tests
pnpm dev
# run accessibility audit (computed contrast, axe-core, target sizes, focus, motion, reflow)
pnpm test:a11y
# render screenshots of the ui
pnpm screenshot
# re-render the screenshots in assets/ (autofix.ci keeps these current)
pnpm assets
# experiment in the playground
pnpm play
pnpm play:viteCredits
This was inspired by youch, which is a phenomenal error page generator, and I would highly recommend it!
License
Made with ❤️
Published under MIT License.
