docmirror/dev-sidecar · info

DevSidecar: script '${filename}' not found

Error message

DevSidecar: script '${filename}' not found

What it means

InsertScriptMiddleware serves locally installed userscripts over the proxy at a ds_script URL path. When the requested filename is not present in the script directory (monkey.get(setting.script.defaultDir)), the middleware responds 404 with this plain-text body instead of throwing. It is an intentional HTTP-level 'resource not found'.

Source

Thrown at packages/mitmproxy/src/lib/proxy/middleware/InsertScriptMiddleware.js:157

  requestIntercept (context, req, res, ssl, next) {
    const { rOptions, log, setting } = context
    if (rOptions.path.indexOf(contextPath) !== 0) {
      return
    }
    const urlPath = rOptions.path
    let filename = urlPath.replace(contextPath, '')

    // 重命名过,向下兼容
    if (filename === 'global') {
      filename = 'tampermonkey'
    }

    const script = monkey.get(setting.script.defaultDir)[filename]
    // log.info(`urlPath: ${urlPath}, fileName: ${filename}, script: ${script}`)

    log.info('ds_script, filename:', filename, ', `script != null` =', script != null)
    if (script == null) {
      res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' })
      res.end(`DevSidecar: script '${filename}' not found`)
      return true
    }
    const now = new Date()
    res.writeHead(200, {
      'DS-Middleware': 'ds_script',
      'Content-Type': 'application/javascript; charset=utf-8',
      'Cache-Control': 'public, max-age=86401, immutable', // 缓存1天
      'Last-Modified': now.toUTCString(),
      'Expires': new Date(now.getTime() + 86400000).toUTCString(), // 缓存1天
      'Date': now.toUTCString(),
    })
    res.write(script.script)
    res.end()
    return true
  },
  responseInterceptor (req, res, proxyReq, proxyRes, ssl, next, append) {
    if (append == null || (!append.head && !append.body)) {

View on GitHub (pinned to 7710cd56cc)

Solutions

  1. Check setting.script.defaultDir (default under ~/.dev-sidecar) and confirm the requested filename exists there
  2. Re-create or re-download the missing userscript into the scripts directory
  3. Clear browser/app cache so the stale reference to the deleted script is re-fetched
  4. On Linux/macOS verify exact filename casing matches the requested name
Defensive patterns

Strategy: validation

Validate before calling

const fs = require('fs'); const path = require('path')
function scriptExists(defaultDir, filename) {
  return Boolean(filename) && fs.existsSync(path.join(defaultDir, filename))
}

Type guard

function isScriptNotFoundResponse(res) {
  return res.statusCode === 404 && typeof res.body === 'string' && res.body.startsWith("DevSidecar: script '")
}

Try / catch

const res = await fetch(scriptUrl, { agent: proxyAgent })
if (res.status === 404 && (await res.text()).includes('not found')) {
  console.warn('Userscript missing on proxy host; reinstalling...')
  await reinstallUserscript(filename)
}

Prevention

When it happens

Trigger: A page or injected <script src> references a userscript filename served through the proxy that does not exist in the script default directory — e.g. the script was deleted/renamed on disk while a cached page still references it, or the URL filename is misspelled/encoded oddly.

Common situations: Stale HTML referencing removed scripts; scripts directory (~/.dev-sidecar/scripts or configured defaultDir) reset or moved; case-sensitivity mismatch on Linux (file 'Foo.js' requested as 'foo.js').


AI-assisted analysis of docmirror/dev-sidecar@7710cd56cc (2026-08-31). Data as JSON: /api/errors/8c99a0b188c1cf5c. Report an issue: GitHub.