nodejs/node · error

Could not open codepage [%s]: %s

Error message

Could not open codepage [%s]: %s

What it means

Emitted from a defensive check near the end of ucbuf_open's setup phase: if a failure status somehow persists after buffer allocation, it prints the requested codepage name and the ICU error name (u_errorName), closes the buffer, and returns nullptr. In practice it mirrors the ucnv_open(*cp) failure path -- the requested converter name is unknown to ICU's converter registry.

Source

Thrown at deps/icu-small/source/tools/toolutil/ucbuf.cpp:516

        if((buf->conv==nullptr) && (buf->showWarning==true)){
            fprintf(stderr,"###WARNING: No converter defined. Using codepage of system.\n");
        }
        buf->remaining=fileSize-buf->signatureLength;
        if(buf->isBuffered){
            buf->bufCapacity=MAX_U_BUF;
        }else{
            buf->bufCapacity=buf->remaining+buf->signatureLength+1/*for terminating nul*/;               
        }
        buf->buffer=(char16_t*) uprv_malloc(U_SIZEOF_UCHAR * buf->bufCapacity );
        if (buf->buffer == nullptr) {
            *error = U_MEMORY_ALLOCATION_ERROR;
            ucbuf_close(buf);
            return nullptr;
        }
        buf->currentPos=buf->buffer;
        buf->bufLimit=buf->buffer;
        if(U_FAILURE(*error)){
            fprintf(stderr, "Could not open codepage [%s]: %s\n", *cp, u_errorName(*error));
            ucbuf_close(buf);
            return nullptr;
        }
        ucbuf_fillucbuf(buf,error);
        if(U_FAILURE(*error)){
            ucbuf_close(buf);
            return nullptr;
        }
        return buf;
    }
    *error =U_FILE_ACCESS_ERROR;
    return nullptr;
}



/* TODO: this method will fail if at the
 * beginning of buffer and the uchar to unget

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Use a canonical ICU converter name/alias: "UTF-8", "ISO-8859-1", "windows-1252", "Shift_JIS", etc. (verify with ucnv_openAllNames or the ICU converter guide).
  2. Read the error name in the printed message to pinpoint the failure, then fix *cp accordingly.
  3. If a required legacy codepage is missing, rebuild ICU with full converter data rather than substituting an approximation.
  4. Pre-check standalone: call ucnv_open(*cp, &err) and confirm U_SUCCESS before invoking ucbuf_open, and reset status = U_ZERO_ERROR before each ICU call.

Example fix

// before: typo'd name -> ucnv_open fails -> "Could not open codepage"
const char* cp = "UTG-8";
UCHARBUF* b = ucbuf_open(path, &cp, true, false, &status);
// after:
const char* cp = "UTF-8";
UCHARBUF* b = ucbuf_open(path, &cp, true, false, &status);
Defensive patterns

Strategy: validation

Validate before calling

// Standalone pre-check: does ICU know this converter name?
#include <unicode/ucnv.h>
bool cp_openable(const char* cp) {
    if (!cp || !*cp) return false;
    UErrorCode err = U_ZERO_ERROR;
    UConverter* c = ucnv_open(cp, &err);
    bool ok = U_SUCCESS(err) && c != nullptr;
    if (c) ucnv_close(c);
    return ok;
}
// call before ucbuf_open; bail out with a clear message if false

Try / catch

UErrorCode status = U_ZERO_ERROR;
const char* cp = argv[enc_idx];
UCHARBUF* b = ucbuf_open(path, &cp, /*showWarning=*/true, false, &status);
if (status == U_FILE_ACCESS_ERROR || U_FAILURE(status)) {
    // 'Could not open codepage' already printed; report and abort.
    fprintf(stderr, "unknown encoding '%s'; see ucnv_openAllNames()\n", cp);
    return EXIT_FAILURE;
}

Prevention

When it happens

Trigger: ucbuf_open called with a *cp string ICU does not recognize: a typo ("UTG-8", "utf 8"), an unsupported/unregistered alias, a MIME label not compiled in, or any name for which ucnv_open sets a failure status. Also reachable if an earlier step in the open sequence set a non-zero status that propagated here.

Common situations: Misspelled encoding passed on the command line / in build config; ICU built with a reduced converter set (legacy EBCDIC/Asian codepages omitted); passing a private or platform-specific alias; status not reset to U_ZERO_ERROR between ICU calls so an earlier failure is mis-attributed to the codepage.

Related errors


AI-assisted analysis of nodejs/node@1b2de5e052 (2026-08-13). Data as JSON: /api/errors/7a664f3c9a06d084. Report an issue: GitHub.