{"id":12006,"library":"server-destroy","title":"Server Destroy","description":"server-destroy is a lightweight Node.js utility designed to enhance `http.Server` or `net.Server` instances with a `destroy()` method. This method allows for the graceful shutdown of a server by not only stopping it from accepting new connections but also forcibly closing all existing open connections, a feature not natively present in older Node.js versions. The package is stable at version 1.0.1 but has not seen updates since its last publish in October 2015, making it effectively abandoned. Its release cadence is non-existent. Key differentiators at the time of its release were providing a complete server shutdown mechanism beyond `server.close()`, which only stops listening for new connections. With Node.js v18.2.0 and later, similar functionality is available natively via `server.closeAllConnections()`. Therefore, this package is primarily useful for projects targeting older Node.js runtimes.","status":"abandoned","version":"1.0.1","language":"javascript","source_language":"en","source_url":"git://github.com/isaacs/server-destroy","tags":["javascript"],"install":[{"cmd":"npm install server-destroy","lang":"bash","label":"npm"},{"cmd":"yarn add server-destroy","lang":"bash","label":"yarn"},{"cmd":"pnpm add server-destroy","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is CommonJS-only and does not provide an ESM export. Use `require` for compatibility.","wrong":"import enableDestroy from 'server-destroy';","symbol":"enableDestroy","correct":"const enableDestroy = require('server-destroy');"},{"note":"While the package itself is JavaScript, TypeScript type definitions are available via `@types/server-destroy` which extends the `net.Server` interface.","symbol":"Server","correct":"/// <reference types=\"node\" />\nimport * as net from 'net';\n// ... enableDestroy(server as net.Server);"}],"quickstart":{"code":"const http = require('http');\nconst enableDestroy = require('server-destroy');\n\nconst PORT = process.env.PORT || 3000;\n\nconst server = http.createServer((req, res) => {\n  console.log(`Request received for ${req.url}`);\n  res.writeHead(200, { 'Content-Type': 'text/plain' });\n  res.end('Hello from server-destroy demo!');\n});\n\n// Enhance the server with a 'destroy' function\nenableDestroy(server);\n\nserver.listen(PORT, () => {\n  console.log(`Server listening on port ${PORT}`);\n});\n\n// Simulate a shutdown after a few seconds\nsetTimeout(() => {\n  console.log('Initiating server shutdown...');\n  server.destroy((err) => {\n    if (err) {\n      console.error('Error during server destruction:', err);\n    } else {\n      console.log('Server and all connections destroyed successfully.');\n      process.exit(0);\n    }\n  });\n}, 5000);\n\n// Handle process exit signals to ensure clean shutdown\nprocess.on('SIGTERM', () => server.destroy(() => process.exit(0)));\nprocess.on('SIGINT', () => server.destroy(() => process.exit(0)));","lang":"javascript","description":"This example demonstrates how to create an HTTP server, enhance it with the `destroy` method using `server-destroy`, and then gracefully shut it down, closing all active connections after 5 seconds. It also shows basic signal handling."},"warnings":[{"fix":"Use `const enableDestroy = require('server-destroy');` in your CommonJS modules. If you are in an ESM project, consider using Node.js's native `server.closeAllConnections()` available in Node.js 18.2.0+ as an alternative.","message":"This package is CommonJS-only (`require()`). Attempting to use `import` syntax in an ES Module project will result in runtime errors. Ensure your project is configured for CommonJS or use a CJS wrapper if absolutely necessary.","severity":"gotcha","affected_versions":"1.0.1"},{"fix":"For new projects or when upgrading Node.js versions, consider migrating to Node.js's native `server.closeAllConnections()` method (available in Node.js v18.2.0 and later) which provides similar functionality.","message":"The `server-destroy` package has not been updated since October 2015 and is no longer actively maintained. While it may still function for its intended purpose, it will not receive bug fixes, security updates, or compatibility improvements for newer Node.js versions or evolving ecosystem standards.","severity":"deprecated","affected_versions":"1.0.1"},{"fix":"If targeting Node.js >=18.2.0, switch to using `server.closeAllConnections()` for graceful server shutdown. For older Node.js versions, `server-destroy` remains a viable option.","message":"Node.js versions 18.2.0 and higher now include a native `server.closeAllConnections()` method, which offers similar functionality to `server-destroy` for closing all active connections. For modern applications, using the built-in method is generally preferred to reduce external dependencies and ensure compatibility with the Node.js core.","severity":"gotcha","affected_versions":"<18.2.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure your file is a CommonJS module (e.g., `.js` file without `\"type\": \"module\"` in `package.json`, or explicitly using `.cjs` extension). If you must use ESM, for Node.js v18.2.0+, consider using the native `server.closeAllConnections()`.","cause":"You are attempting to use the CommonJS `require()` function in an ECMAScript Module (ESM) file or project.","error":"ReferenceError: require is not defined in ES module scope"},{"fix":"Ensure your server's shutdown logic explicitly calls `server.destroy()` (or `server.closeAllConnections()` for newer Node.js versions) to terminate all open connections. For lingering processes, use `kill -9 <PID>` on Linux/macOS or Task Manager on Windows to manually terminate the process.","cause":"A previous server instance or another process is still holding the port open, often because active connections were not properly closed during shutdown.","error":"Error: listen EADDRINUSE: address already in use :::<port_number>"}],"ecosystem":"npm"}