{"record":{"id":"05ad4c5f12527dd5","repo":"nodejs/node","slug":"could-not-find-pattern-matching-s-05ad4c","errorCode":null,"errorMessage":"Could not find pattern matching %s","messagePattern":"Could not find pattern matching (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"tools/getnapibuildversion.py","lineNumber":22,"sourceCode":"\n\ndef get_napi_version():\n  napi_version_h = os.path.join(\n    os.path.dirname(__file__),\n    '..',\n    'src',\n    'node_version.h')\n\n  f = open(napi_version_h)\n\n  regex = '^#define NODE_API_SUPPORTED_VERSION_MAX'\n\n  for line in f:\n    if re.match(regex, line):\n      napi_version = line.split()[2]\n      return napi_version\n\n  raise Exception('Could not find pattern matching %s' % regex)\n\n\nif __name__ == '__main__':\n  print(get_napi_version())\n","sourceCodeStart":4,"sourceCodeEnd":27,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/tools/getnapibuildversion.py#L4-L27","documentation":"Raised by get_napi_version() in tools/getnapibuildversion.py after it opens ../src/node_version.h (relative to the script) and iterates every line without finding one matching '^#define NODE_API_SUPPORTED_VERSION_MAX'. The file was readable, but the expected N-API version macro is absent. This script feeds Node's native-addon build system the maximum stable N-API version, so a missing macro means the build cannot pick an N-API target.","triggerScenarios":"Calling `python tools/getnapibuildversion.py` (or having node-gyp/gyp invoke it during a native module build) when the checked-out src/node_version.h predates N-API, has been stripped, or the script is run from a tree where src/ is not the matching Node source. The regex matches only a literal '#define NODE_API_SUPPORTED_VERSION_MAX' line; any reformatting (e.g. extra spaces, renamed macro) also misses.","commonSituations":"Building against an old Node checkout (pre-8.6.0) that never had the macro; running the tool standalone outside a Node source tree; vendoring only part of Node's src/; a downstream fork that renamed the macro.","solutions":["Open src/node_version.h and confirm a line exactly like '#define NODE_API_SUPPORTED_VERSION_VERSION_MAX 9' exists; if missing, update the Node checkout.","Ensure the script is run from the correct repo root so '../src/node_version.h' resolves to the real Node header (check `os.path.dirname(__file__)/../src/node_version.h`).","If you maintain a fork that renamed the macro, patch the regex at getnapibuildversion.py:15 to match the new name.","Pin to a Node version known to ship the macro rather than a stripped/custom header."],"exampleFix":"// before\nregex = '^#define NODE_API_SUPPORTED_VERSION_MAX'\n\n// after (also accept a legacy/renamed macro, fail loudly with file context)\nregex = r'^#\\s*define\\s+NODE_API_SUPPORTED_VERSION_MAX\\b'\nif not os.path.exists(napi_version_h):\n  raise Exception('node_version.h not found at %s' % napi_version_h)","handlingStrategy":"validation","validationCode":"import os, re\nh = os.path.join(os.path.dirname(__file__), 'src', 'node_version.h')\nif not os.path.exists(h):\n    raise SystemExit('node_version.h missing at %s' % h)\nfound = any(re.match(r'^#define\\s+NODE_API_SUPPORTED_VERSION_MAX\\b', line)\n            for line in open(h))\nif not found:\n    raise SystemExit('macro not present; update Node checkout')","typeGuard":"def has_napi_macro(header_path: str) -> bool:\n    import re\n    if not os.path.isfile(header_path):\n        return False\n    pat = re.compile(r'^#define\\s+NODE_API_SUPPORTED_VERSION_MAX\\b')\n    return any(pat.match(line) for line in open(header_path))","tryCatchPattern":"try:\n    napi = get_napi_version()\nexcept Exception as e:\n    # header present but macro absent -> wrong/old Node source\n    raise SystemExit('Cannot determine N-API version: %s. Check src/node_version.h.' % e)","preventionTips":["Pin the Node source checkout to a version known to ship NODE_API_SUPPORTED_VERSION_MAX (>= 8.6.0).","Run getnapibuildversion.py from the repo root so the relative ../src path resolves correctly.","In CI, assert the macro exists before invoking the native build step."],"tags":["nodejs","native-addons","build","napi","config"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}