Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Node.js crashes with a TypeError while trying to set the exit code

Your error

TypeError [ERR_INVALID_ARG_TYPE]: The "code" argument must be of type number. Received type string ('P1001')2 more lines it can show

Fix it fast

Most common cause, 1 of 3

An error's string code is passed as the exit code

Log the error code where people can read it and pass a numeric exit status, usually 1 for failure.

Fix
bootstrap().catch((err) => {
  console.error('Startup failed:', err.code, err.message);
  process.exitCode = 1;
});
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

TypeError [ERR_INVALID_ARG_TYPE]: The "code" argument must be of type number appears when process.exit() or process.exitCode gets something other than an integer. Since Node.js 20, an error code string such as 'P1001' or a whole Error object is rejected. The value after Received is the original failure. Log it and exit with 1 instead. Applies to Node.js 20 to 26.

  • Applies to Node.js 26.13.1, Node.js 24.22.3, Node.js 22.23.5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Run the app once with a longer stack trace and look at the frame just below process.exit. When that frame sits in your own source, read causes 1 and 2; when it sits under node_modules, the culprit is a dependency (cause 3).

Run this first
node --stack-trace-limit=50 dist/main.js 2>&1 | grep -A3 "at process.exit"

Then find the line you see in this list and open its fix:

  1. 1An error's string code is passed as the exit codeReceived type string followed by an error label such as ('P1001'), ('ECONNREFUSED') or ('ENOENT')
  2. 2The Error object itself is handed to process.exitReceived an instance of Error, or of a subclass such as PrismaClientInitializationError
  3. 3A dependency still relies on the coercion removed in Node.js 20The frame under process.exit is a file in node_modules, for example a CLI's watch or close handler

What does the error look like?

  • TypeError [ERR_INVALID_ARG_TYPE]: The "code" argument must be of type number. Received type string ('P1001')
  • TypeError [ERR_INVALID_ARG_TYPE]: The "code" argument must be of type number. Received an instance of Error
  • TypeError: The "code" argument must be of type number. Received type string ('ENOENT')

Cause 1 of 3

An error's string code is passed as the exit code

You seeReceived type string followed by an error label such as ('P1001'), ('ECONNREFUSED') or ('ENOENT')

Check

This TypeError comes from Node.js refusing a string exit code. Error codes are string labels in Node.js and in libraries such as Prisma, so a handler that does process.exit(err.code) hands over text. Since v20, process.exit() accepts only integers or strings that represent an integer, and the same rule applies to process.exitCode.

Check
grep -rnE "process\.exit\(|process\.exitCode" src

Fix

Log the error code where people can read it and pass a numeric exit status, usually 1 for failure.

Fix
bootstrap().catch((err) => {
  console.error('Startup failed:', err.code, err.message);
  process.exitCode = 1;
});

Verify

The process ends with your log line and exit status 1, and the TypeError is gone.

Verify
node dist/main.js; echo "exit status: $?"

Confirmed by nodejs.org

Did this fix it?

Cause 2 of 3

The Error object itself is handed to process.exit

You seeReceived an instance of Error, or of a subclass such as PrismaClientInitializationError

Check

An instance of Error in the message means an exception object went where a number belongs. The usual forms are promise.catch(process.exit) or an uncaughtException listener that forwards its first argument to process.exit, since both pass the error as the code.

Check
grep -rnE "catch\(process\.exit\)|'uncaughtException'" src

Fix

Wrap the exit in a small function that prints the error and then exits with a number, so the stack trace of the real failure is kept.

Fix
main().catch((err) => {
  console.error(err);
  process.exit(1);
});

Verify

The original error and its stack trace print first, followed by exit status 1.

Verify
node dist/main.js; echo "exit status: $?"

Confirmed by github.com

Did this fix it?

Cause 3 of 3

A dependency still relies on the coercion removed in Node.js 20

You seeThe frame under process.exit is a file in node_modules, for example a CLI's watch or close handler

Check

Node.js used to convert odd exit codes to integers. That coercion became a runtime deprecation in v19 and reached End-of-Life in v20 as DEP0164, so tools written before then can now crash with this TypeError while they handle some other failure.

Check
npm ls rollup

Fix

Upgrade the package to a release that passes numeric codes. Until a fix exists, treat the value after Received as the real problem and solve that, for example a missing file or a watcher limit, since it is what the tool was trying to report.

Fix
npm install --save-dev rollup@latest
npm ls rollup

Verify

The tool either runs or prints its own readable error instead of the TypeError.

Verify
npx rollup --version

Confirmed by nodejs.org

Did this fix it?

How do I stop it coming back?

  • Keep exit handling in one helper that logs the error and sets process.exitCode to a fixed number, and call it from every catch at the top level.
  • Search pull requests for process.exit(err) and process.exit(err.code) with a grep step in CI, and fail the job on any hit.
  • Run CI on the same Node.js major as production, so a stricter runtime rule fails in a pull request and not on deploy.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, Workforce.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.