nodejs/node · error · GypError

Missing type field for target %s in %s.

Error message

Missing type field for target %s in %s.

What it means

Raised as a GypError when a target has no 'type' field at all. This is the companion to the invalid-type error: the dict lookup raises KeyError, and because spec.get('type') is falsy (missing or None), the 'Missing type field' branch is taken instead of the invalid-type branch. A target without a type cannot be mapped to any MSVS project kind.

Source

Thrown at tools/gyp/pylib/gyp/generator/msvs.py:1152

        An integer, the configuration type.
    """
    try:
        config_type = {
            "executable": "1",  # .exe
            "shared_library": "2",  # .dll
            "loadable_module": "2",  # .dll
            "static_library": "4",  # .lib
            "windows_driver": "5",  # .sys
            "none": "10",  # Utility type
        }[spec["type"]]
    except KeyError:
        if spec.get("type"):
            raise GypError(
                "Target type %s is not a valid target type for "
                "target %s in %s." % (spec["type"], spec["target_name"], build_file)
            )
        else:
            raise GypError(
                "Missing type field for target %s in %s."
                % (spec["target_name"], build_file)
            )
    return config_type


def _AddConfigurationToMSVSProject(p, spec, config_type, config_name, config):
    """Adds a configuration to the MSVS project.

    Many settings in a vcproj file are specific to a configuration.  This
    function the main part of the vcproj file that's configuration specific.

    Arguments:
      p: The target project being generated.
      spec: The target dictionary containing the properties of the target.
      config_type: The configuration type, a number as defined by Microsoft.
      config_name: The name of the configuration.
      config: The dictionary that defines the special processing to be done

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Add a 'type' key to the target with a valid value (e.g. 'static_library', 'executable', 'none').
  2. If the target is templated, ensure the template/conditions always produce a concrete type.
  3. Run gyp's check mode to catch missing required fields early.

Example fix

// before
'targets': [{ 'target_name': 'foo', 'sources': ['foo.cc'] }]
// after
'targets': [{ 'target_name': 'foo', 'type': 'static_library', 'sources': ['foo.cc'] }]
Defensive patterns

Strategy: validation

Validate before calling

for t in build_file_data.get('targets', []):
    if not t.get('type'):
        raise ValueError(f"target {t.get('target_name')} missing 'type'")

Type guard

def target_has_type(t) -> bool:
    return isinstance(t, dict) and bool(t.get('type'))

Prevention

When it happens

Trigger: A target dictionary in a .gyp file that omits the 'type' key entirely, or sets it to None/empty. The KeyError at msvs.py:1138 is caught and spec.get('type') evaluates falsy, hitting msvs.py:1153.

Common situations: A newly added target where 'type' was forgotten; a target constructed via a template/include that failed to set type; accidental deletion of the type line during a refactor.

Related errors


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