E2BIG

Fix Node.js 'spawn E2BIG' Error – Arguments Too Long

Programming & Dev Tools Intermediate 👁 8 views 📅 Jun 28, 2026

This error means your command arguments are too big. Happens when spawning a process with a huge string. Quick fix: pass data via stdin or file instead.

You're running a Node.js script that spawns a child process. Maybe it's child_process.spawn('grep', ['-r', someBigString]) or spawn('sh', ['-c', giantCommand]). Then boom – you get Error: spawn E2BIG. This isn't random. It happens when the total size of your command arguments plus environment variables is bigger than what the OS allows. On Linux, that limit is usually 2MB (2097152 bytes). On macOS, it's around 256KB. On Windows, different story – but you'll hit similar limits.

Why you're hitting E2BIG

The operating system puts a hard cap on how much data you can pass to a new process through exec/spawn arguments. This includes the command name, all arguments, and environment variables. When you cross that line, the kernel says "no" and returns E2BIG – "Argument list too long." The real trigger is almost always trying to pass a massive string (like file contents, a huge JSON blob, or a list of thousands of filenames) directly as a command argument. I've seen this happen when someone tries to pass a 10MB log file as a grep pattern. Don't do that.

The fix: stop passing data through arguments

You've got two solid options. Both avoid the OS argument limit entirely. Pick the one that fits your situation.

Option 1: Pass data through stdin

Instead of putting the big string in the command arguments, pipe it through stdin. This works for programs that read from stdin (like grep, sed, awk, and most Unix tools with - flag).

  1. Open your Node.js script where you're calling spawn.
  2. Change from this:
    // This will FAIL if someBigString is too big
    const child = spawn('grep', ['-r', someBigString], { cwd: '/target' });
    child.stdout.on('data', (data) => console.log(data.toString()));
  3. To this – feed the big string through stdin:
    // Better: use stdin
    const child = spawn('grep', ['-r', '-'], { cwd: '/target' });
    child.stdin.write(someBigString);
    child.stdin.end();
    child.stdout.on('data', (data) => console.log(data.toString()));
  4. Note the hyphen - in the arguments. That tells grep to read from stdin. Other tools may use a different flag (like --file=- for curl). Check the man page.
  5. After you change it and run the script again, expect the spawn to work without the E2BIG error. The data flows through the pipe, not the argument list.

Option 2: Write data to a temp file, then pass the filename

If the program you're calling doesn't support stdin (like some older tools), write your big string to a temporary file and pass just the file path as an argument. That path is small – no E2BIG.

  1. First, write the big data to a temp file. Use Node's fs.writeFileSync or fs.promises.writeFile. I recommend the os.tmpdir() path.
    const fs = require('fs');
    const os = require('os');
    const path = require('path');
    
    const tempFile = path.join(os.tmpdir(), 'bigdata-' + Date.now() + '.txt');
    fs.writeFileSync(tempFile, someBigString);
  2. Now spawn your process with just the filename (which is short):
    const child = spawn('some-tool', ['--file', tempFile]);
    child.stdout.on('data', (data) => console.log(data.toString()));
  3. After the process finishes, clean up the temp file:
    child.on('exit', () => {
      fs.unlinkSync(tempFile);
    });
  4. When you run this, the temp file gets created, the process reads it, and you get no E2BIG. The argument list stays tiny.

What if the error still shows up?

If you've switched to stdin or a temp file and still see E2BIG, check two things:

  • Environment variables are too large. The OS limit includes environment variables. If your process.env is huge (like a massive PATH or some custom variable), you can pass a trimmed environment in the spawn options:
    const child = spawn('cmd', ['arg'], {
      env: { PATH: '/usr/bin', HOME: '/home/user' }  // only send what's needed
    });
  • You're still passing data as arguments by accident. Double-check your code. I've seen people fix the main argument but leave a second huge string in another argument. Print the whole command with console.log(child.spawnargs) to see what you're actually sending.

One more thing: on Windows, the limit is around 32,767 characters for the command line. Same fix applies – use stdin or a file. The OS doesn't care about your data size. It just cares about the argument list size.

Was this solution helpful?