Skip to content

Instantly share code, notes, and snippets.

@mickert
Last active January 22, 2026 22:14
Show Gist options
  • Select an option

  • Save mickert/786a03707de3eaaee3b34a350409fb6d to your computer and use it in GitHub Desktop.

Select an option

Save mickert/786a03707de3eaaee3b34a350409fb6d to your computer and use it in GitHub Desktop.
Directus 9.9 → 10+ startup patch for IIS, IISNode, Passenger (Plesk), Windows named pipes, and UNIX sockets (ERR_SOCKET_BAD_PORT fix). Includes workaround for “Package subpath './server' is not defined by exports” and DEP0005 deprecation warnings.
/**
* Directus Server Bootstrap Patch & Deployment Fixes (Directus 9.9 → 10+)
* -----------------------------------------------------------------------
* A consolidated workaround for multiple Directus startup and deployment
* issues commonly encountered on Passenger (Plesk), IISNode, Windows named
* pipes, and UNIX socket environments.
*
* This patch improves reliability when running Directus behind reverse
* proxies, on shared hosting, or in environments where Directus incorrectly
* interprets named pipes as numeric ports. It also filters noisy Node.js
* deprecation warnings (e.g., DEP0005) that affect IISNode deployments.
*
* Fixes and keywords for discoverability:
* - Directus fails to start on named pipes / IISNode
* - “RangeError [ERR_SOCKET_BAD_PORT]: options.port should be >= 0…”
* - “Package subpath './server' is not defined by exports”
* - Directus 9.x → 10.x migration issues
* - Passenger / Plesk deployment errors
* - Directus server.js patch / startServer override
* - Node.js port parsing bug with Directus
*
* Upstream references:
* - Export regression: https://github.com/directus/directus/issues/18700
* - Passenger deployment failure: https://github.com/directus/directus/issues/9613
* - Named pipe / UNIX socket startup failure: https://github.com/directus/directus/issues/5977
*
* Includes a re-rolled version of the community fix by @stylefieber (issue #18307)
* and a reference implementation for environments requiring patched startServer()
* behavior.
*
* Proposed upstream fix:
* https://github.com/mickert/directus/blob/issue-5977-iisnode-namedpipes/api/src/server.ts
*
* Gist reference (canonical source):
* https://gists.mickert.dev/786a03707de3eaaee3b34a350409fb6d
*
* ---------------------------------------------------------------------------
*
* If this patch or documentation has been useful, you can explore more of my
* work and support future development via:
* https://gists.mickert.dev/
* https://github.mickert.dev/
* https://support.mickert.dev/
*
*
* ---------------------------------------------------------------------------
* LICENSE NOTICE — MULTIPLE LICENSES APPLY
* ---------------------------------------------------------------------------
*
* This gist contains two separately licensed parts:
*
* 1. Original Work (wrapper code)
* Copyright © 2024–2026 Michel Sijmons
* Licensed under the Apache License, Version 2.0.
* You may obtain a copy of the license at:
* https://www.apache.org/licenses/LICENSE-2.0
*
* 2. Directus-Derived Code (patched startServer implementation)
* This section is derived from Directus source code and remains licensed
* under the Business Source License 1.1 (BSL 1.1), as required by upstream.
* Directus license:
* https://github.com/directus/directus/blob/main/license
*
* Do not relicense or remove the BSL notice from the Directus-derived section.
* Only the wrapper code is Apache-2.0 licensed.
*
* ---------------------------------------------------------------------------
* Author: Michel Sijmons — https://mickert.dev
*/
// ---------------------------------------------------------------------------
// QUICK FIX - COPY & PASTE
// ---------------------------------------------------------------------------
// If Directus fails to start on IISNode or named pipes with:
//
// RangeError [ERR_SOCKET_BAD_PORT]:
// options.port should be >= 0 and < 65536. Received type number (NaN).
//
// then your environment is passing a named pipe (e.g. \\.\pipe\something)
// through the PORT variable, and Directus incorrectly treats it as a number.
//
// To fix this, replace the startServer() function in:
//
// node_modules/@directus/api/dist/server.js
//
// with the patched implementation shown below in the
// “DIRECTUS-DERIVED CODE - LICENSED UNDER BSL 1.1” section.
//
// This patch:
// - Detects named pipes correctly
// - Avoids NaN port parsing
// - Supports IISNode + Windows named pipes
// - Supports UNIX socket paths
// - Preserves normal TCP host/port behavior
//
// Full reference implementation:
// https://github.com/mickert/directus/blob/issue-5977-iisnode-namedpipes/api/src/server.ts
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// APACHE-2.0 licensed WRAPPER CODE (ORIGINAL WORK)
// ---------------------------------------------------------------------------
(async () => {
// Filter deprecation warnings (e.g., IISNode DEP0005 noise)
const emitWarningOrg = process.emitWarning;
process.emitWarning = (...args) => {
if (args[2] !== 'DEP0005') {
return emitWarningOrg.apply(process, args);
}
};
// Import Directus server and start it
console.log('Server starting....');
(await import('@directus/api/server')).startServer();
})();
// ---------------------------------------------------------------------------
// DIRECTUS-DERIVED CODE - LICENSED UNDER BSL 1.1
// ---------------------------------------------------------------------------
// The following function is a modified excerpt of Directus source code.
// It is NOT licensed under Apache-2.0. It remains under the Directus Business
// Source License 1.1 (BSL 1.1) as required by the upstream project.
//
// Original source:
// https://github.com/directus/directus
//
// Directus License:
// https://github.com/directus/directus/blob/main/license
//
// This patch resolves:
// - Named pipe handling issues on IISNode
// - “RangeError [ERR_SOCKET_BAD_PORT]: options.port should be >= 0…”
// - Incorrect interpretation of named pipes as numeric ports
//
// Reference implementation:
// https://github.com/mickert/directus/blob/issue-5977-iisnode-namedpipes/api/src/server.ts
// ---------------------------------------------------------------------------
/*
export async function startServer() {
const server = await createServer();
const host = env['HOST'];
let path = env['UNIX_SOCKET_PATH'];
const port = env['PORT'];
let listenOptions;
if (!path && port && port.length && isNaN(port)) {
path = port;
}
if (path) {
listenOptions = { path };
} else {
listenOptions = {
host,
port: parseInt(port),
};
}
server
.listen(listenOptions, () => {
const protocol = server instanceof https.Server ? 'https' : 'http';
logger.info(
`Server started at ${path ? path : `${protocol}://${host}:${port}`}`
);
process.send?.('ready');
emitter.emitAction(
'server.start',
{ server },
{
database: getDatabase(),
schema: null,
accountability: null,
},
);
})
.once('error', (err) => {
if (err?.code === 'EADDRINUSE') {
logger.error(`${listenOptions.port ? `${host}:${listenOptions.port}` : path} is already in use`);
process.exit(1);
} else {
throw err;
}
});
}
*/
/**
* Canonical gist URL:
* https://gists.mickert.dev/786a03707de3eaaee3b34a350409fb6d
*
* If this patch or documentation has been useful, you can explore more of my
* work and support future development via:
* https://gists.mickert.dev/
* https://github.mickert.dev/
* https://support.mickert.dev/
*
* Licensing:
* Wrapper code is licensed under the Apache License 2.0.
* Directus-derived code remains under the Business Source License 1.1.
*
* For full details, see the license header at the top of this file.
*/
@mickert

mickert commented Jan 17, 2026 •

Copy link
Copy Markdown
Author

Pinned Comment — Important Notes, Quick Start & Licensing

This gist provides a startup patch for Directus 9.9 → 10+, fixing named‑pipe port parsing issues and startup failures on IIS, IISNode, Passenger (Plesk), Windows named pipes, and UNIX sockets.
It also includes workarounds for ERR_SOCKET_BAD_PORT,
“Package subpath './server' is not defined by exports”, and DEP0005 deprecation warnings.

📄 Main script file:
directus-startserver-named-pipe-port-fix.js

📄 Canonical gist URL:
https://gists.mickert.dev/786a03707de3eaaee3b34a350409fb6d


What this gist contains

A wrapper script that patches Directus startup behavior, a Directus-derived reference implementation of startServer(), and instructions for applying the patch in environments where Directus fails to start.

If Directus fails to start with errors like:

  • RangeError [ERR_SOCKET_BAD_PORT]
  • Named-pipe misdetection
  • Passenger / Plesk startup failures

…follow the Quick Start and check the “QUICK FIX - COPY & PASTE” section in the gist.


🔧 Quick Start

  1. Locate the Directus API server file:
    node_modules/@directus/api/dist/server.js
  2. Replace the startServer() implementation with the patched version in this gist
  3. Restart Directus
  4. If you are running on IIS, IISNode, Passenger, Plesk, or named pipes, the startup should now succeed without ERR_SOCKET_BAD_PORT or export-related failures

🔍 If the patch doesn’t apply cleanly

Directus updates may change the internal server structure.
If the patch no longer matches, check the upstream references linked in the gist header.


👋 Hey visitor - Michel here

If this patch saved you hours of debugging, unblocked your deployment, or taught you something useful, consider supporting my work:

Your support helps me create more tools, patches, and deep-dives like this.
Happy building!


📜 Licensing

This gist contains two separately licensed parts:

1. Wrapper code - Apache License 2.0
Copyright © 2024-2026
Michel Sijmons - https://mickert.dev

2. Directus-derived code - Business Source License 1.1 (BSL 1.1)
Required by upstream.
Do not relicense or remove the BSL notice.

Important:
Do not relicense Directus code under Apache 2.0.
Do not remove the BSL notice.
You may publish this gist publicly as long as the license separation remains intact.

See the full license header inside the script.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment