All jobs

Fix "TypeError: marked is not a function" After Upgrading marked

Yes. An agent moves a build script from marked's old default export and callback to the named export and the extension packages, then rebuilds the page and diffs it.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

marked 4.0.0 stopped exporting the function itself. require("marked") now returns the module object, with marked, parse, Marked, Lexer, Parser and the rest as properties, so a script written as const marked = require("marked") then marked(text) fails with the TypeError in the title on the first call. Two more things went in later releases and fail quietly instead of loudly: 5.0.0 deprecated the callback form and the highlight, smartypants, mangle and headerIds options with console warnings, and current releases ignore them without a word. On marked 18 marked(text, callback) returns the HTML as a string and never calls the callback, so a script that only writes its output from that callback produces nothing and exits 0.

A small static site like chantastic/reactpatterns has the whole pattern in scripts/readme-to-index.js. It reads README.markdown, calls marked.setOptions with smartypants: true and a highlight function that hands each code block to pygmentize-bundled through a callback, then calls marked(readme, consTemplate), whose callback renders scripts/_index.html with consolidate's lodash engine and writes index.html. package.json asks for marked ^0.8.0 and yarn.lock holds 0.8.0, so the committed index.html was built with a release from 2019. Install the current marked and node scripts/readme-to-index.js dies at line 33 with the TypeError.

The agent reproduces that, then looks at what else the script relied on and checks each on the installed package rather than from memory. The named export is const { marked } = require("marked"), or new Marked() for an instance with its own options. The callback form has to become await marked.parse(...), and the two options have moved into extension packages: marked-highlight, whose async: true mode takes a highlighter that returns a promise, and marked-smartypants. It also finds that the highlighter itself is dead on a current machine: pygmentize-bundled spawns a python executable with a Pygments copy from the Python 2 era, which fails on Python 3.11 with a regex error before marked is involved. The agent keeps the Pygments output, and with it the .highlight CSS classes in the template's stylesheet, by calling a current pygmentize from the Pygments package instead.

Then it proves the rewrite by rebuilding. The new script produces an index.html with the same 18 section headings as the committed one and the same count of curly quotes, which shows the smartypants extension is active; it reports the counts that moved, a few fewer highlighted blocks and a different number of keyword spans, as a diff for the reviewer to read, since a newer Markdown parser and a newer Pygments both change details. The pull request pins marked and the two extensions in package.json, drops pygmentize-bundled, and notes the Node version the current marked requires.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "The build script in this repository fails with TypeError: marked is not a function after marked was upgraded. Install from the lock file first and record what the script does on the old release, including any failure that is unrelated to marked. Then install the current marked, reproduce the error and quote it. Read the installed package to confirm the named export, and list every option and call form the script uses that the current release no longer honours: the callback argument, highlight, smartypants, mangle, headerIds, sanitize. For each, say whether it is ignored silently or throws, and port it: the named export or a Marked instance, parse with await, and the marked-highlight and marked-smartypants extensions where the script used those options. If the syntax highlighter the script shells out to no longer runs, replace it with one that keeps the same CSS classes the template's stylesheet expects, and say which. Rebuild the page, compare the new output with the committed one by counting headings, code blocks, highlighted spans and smart quotes, and report the differences with a reason for each. Pin marked and the extensions, set the engines field to the Node version marked requires, and open a pull request with the old-run output, the error, the diff and the comparison.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request that rewrites the build script, updates the dependencies in package.json and the lock file, and commits the rebuilt index.html. The description opens with the TypeError and the line it came from, then a table of the five things the old script relied on, the default export, the callback, smartypants, highlight and the bundled highlighter, with what the current release does for each and where it went.

Below that is the comparison of the old and new output: headings, code blocks, highlighted spans and curly quotes, with the counts that match and the ones that moved. Where a count moved the row says why, a fenced block the newer parser reads differently or a token the newer Pygments splits in two, so the reviewer reads the HTML diff with that in mind rather than hunting for a regression.

What a reviewer checks before merging is the highlighter choice and the engine requirement. Calling a system pygmentize keeps the stylesheet but adds a Python dependency to a Node build; the description names that trade and the alternative, a JavaScript highlighter with a different class scheme and a stylesheet change, so the reviewer picks with the cost in view.

Run it on every dependency update that breaks the build

This error usually arrives in a bot's pull request that bumps marked across a major release, where the install passes and the build step dies. An automation's github trigger never fires for a bot sender, so give the automation a webhook trigger with the prompt above and add a step to the workflow that runs only when the build failed and its log contains marked is not a function. Have it post the branch name and the failing lines to the trigger's URL with Authorization: Bearer and the trigger's secret, kept as a repository secret. The posted text is placed after the saved prompt as the event.

Send an Idempotency-Key header to the trigger's URL set to the workflow run id, so a re-run of the failed job starts nothing new. A build that exits 0 and writes nothing is the harder case: add a step that fails when the output file is missing or unchanged, so the silent form of this break reaches the same trigger.

The use case this job follows, with its automation and trigger: Fix a bug from an alert.

Why run it on Rush?

Half of this break is silent. A script that only trips on the TypeError is easy; the one that moved to the named export, kept the callback and now produces no file at all is the one that ships a stale page for months. The agent runs the build in its own sandbox and reads the output file, so a run that exits cleanly but writes nothing is caught, and the session keeps the transcript of each attempt.

The port also crosses two ecosystems, a Node build and a Python highlighter, and proving it means installing both and comparing two generated pages. The agent does that side by side and brings back counts rather than an opinion, so a reviewer who owns the site can accept a slightly different highlight of a keyword with the reason in front of them.

Frequently asked questions

Which release removed the default export?

marked 4.0.0, in November 2021. On 3.x, require("marked") is a function; from 4.0.0 it is an object whose marked and parse properties are the function. The agent checks the installed release directly rather than relying on a changelog.

Why did my build start passing with no output after I fixed the import?

Because the callback form is gone. On current releases marked(text, callback) returns the HTML and ignores the second argument, so a script that writes its file from inside the callback never writes it. The fix is await marked.parse(text) and writing the result.

Where did the highlight and smartypants options go?

Into extension packages, marked-highlight and marked-smartypants, registered with marked.use or passed to new Marked(). The highlight extension has an async: true mode that accepts a highlighter returning a promise, which is what a shell-out needs.

Can I pin marked to 3.x and keep the old script?

It installs and runs, and the agent shows that on the lock file. The 3.x line has had no release since 2021, the callback and option paths it depends on are gone from every supported version, and the bundled highlighter in a script like this one fails on current Python regardless. The pin buys time; the port is the fix.

Will the rebuilt page be byte-identical?

No, and the pull request does not claim it. A newer Markdown parser and a newer highlighter both change details in the HTML. The agent reports what stayed the same, headings and smart quotes, and what moved, with a reason per count, so the review is of known differences.

More jobs like this: Fix a bug from an alert

Keep reading