denoland/deno · error · TypeError

ERR_INVALID_ARG_TYPE

ERR_INVALID_ARG_TYPE

Error message

The "arguments[${pos}]" argument must be of type object. Received ${actual}

What it means

normalizeSpawnArguments implements the overloads spawn/execFile/fork(command[, args][, options]): after consuming an optional args array and skipping null, the next positional argument must be a plain options object. The most common trigger is passing args as a bare string (e.g. spawn('ls', '-la')), which lands in the options slot and fails the object check.

Source

Thrown at ext/node/polyfills/child_process.ts:127

  let execArgv;
  let options: SpawnOptions & {
    execArgv?: string;
    execPath?: string;
    silent?: boolean;
  } = { __proto__: null } as typeof options;
  let args: string[] = [];
  let pos = 1;
  if (pos < arguments.length && ArrayIsArray(arguments[pos])) {
    args = arguments[pos++];
  }

  if (pos < arguments.length && arguments[pos] == null) {
    pos++;
  }

  if (pos < arguments.length && arguments[pos] != null) {
    if (typeof arguments[pos] !== "object" || ArrayIsArray(arguments[pos])) {
      throw new ERR_INVALID_ARG_TYPE(
        `arguments[${pos}]`,
        "object",
        arguments[pos],
      );
    }

    options = { __proto__: null, ...arguments[pos++] } as typeof options;
  }

  // Validate null bytes in args
  for (let i = 0; i < args.length; i++) {
    if (typeof args[i] === "string") {
      validateNullByteNotInArg(args[i], `args[${i}]`);
    }
  }

  // Validate null bytes in execPath
  if (options.execPath != null) {

View on GitHub (pinned to 89f33cbef2)

Solutions

  1. Wrap the arguments in an array: spawn('ls', ['-la'])
  2. Pass options as an object: spawn('cmd', ['-a'], { stdio: 'inherit' })
  3. Use exec() when you genuinely want a full shell command string

Example fix

// before
spawn('ls', '-la');
// after
spawn('ls', ['-la']);
Defensive patterns

Strategy: validation

Validate before calling

const args = Array.isArray(rest) ? rest : [];
const opts = typeof last === 'object' && last !== null && !Array.isArray(last) ? last : {};
spawn(cmd, args, opts);

Type guard

const isSpawnArgs = (a) => a == null || Array.isArray(a);
const isSpawnOptions = (o) => o == null || (typeof o === 'object' && !Array.isArray(o));

Prevention

When it happens

Trigger: spawn('ls', '-la'); spawn('node', 'script.js'); spawn('cmd', ['-a'], 'inherit'); execFile('bin', '--flag'); fork('child.js', '--arg') where a string occupies the arguments[pos] slot.

Common situations: Coming from exec() which accepts a single command string; passing shell-style flag strings; forgetting the array brackets around args; passing an array as options.

Related errors


AI-assisted analysis of denoland/deno@89f33cbef2 (2026-08-16). Data as JSON: /api/errors/cfdf15a691d0686a. Report an issue: GitHub.