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 showFix it fast
Most common cause, 1 of 3An 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.
bootstrap().catch((err) => {
console.error('Startup failed:', err.code, err.message);
process.exitCode = 1;
});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.
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).
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:
- 1An error's string code is passed as the exit code
Received type string followed by an error label such as ('P1001'), ('ECONNREFUSED') or ('ENOENT') - 2The Error object itself is handed to process.exit
Received an instance of Error, or of a subclass such as PrismaClientInitializationError - 3A dependency still relies on the coercion removed in Node.js 20
The 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 ErrorTypeError: 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.
grep -rnE "process\.exit\(|process\.exitCode" srcFix
Log the error code where people can read it and pass a numeric exit status, usually 1 for failure.
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.
node dist/main.js; echo "exit status: $?"Confirmed by nodejs.org
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.
grep -rnE "catch\(process\.exit\)|'uncaughtException'" srcFix
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.
main().catch((err) => {
console.error(err);
process.exit(1);
});Verify
The original error and its stack trace print first, followed by exit status 1.
node dist/main.js; echo "exit status: $?"Confirmed by github.com
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.
npm ls rollupFix
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.
npm install --save-dev rollup@latest
npm ls rollupVerify
The tool either runs or prints its own readable error instead of the TypeError.
npx rollup --versionConfirmed by nodejs.org
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.