{"id":11456,"library":"node-static","title":"Node-Static","description":"`node-static` is a legacy module designed for serving static files over HTTP, adhering to RFC 2616, including support for conditional GET and HEAD requests. The last published version, 0.7.11, dates back over eight years, targeting very old Node.js environments (engine requirement `node >= 0.4.1`). It simplifies static file serving by abstracting `Cache-Control` headers and basic error handling, allowing developers to specify a root directory and cache duration. Due to its age and lack of maintenance, it is now considered abandoned. Its API heavily relies on callback patterns and older Node.js `http` module stream handling, which are not idiomatic in modern JavaScript development, making it unsuitable for contemporary projects.","status":"abandoned","version":"0.7.11","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/cloudhead/node-static","tags":["javascript","http","static","file","server"],"install":[{"cmd":"npm install node-static","lang":"bash","label":"npm"},{"cmd":"yarn add node-static","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-static","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is CommonJS-only and relies on direct instantiation from the required module. Attempting to use ESM `import` will result in an `ERR_REQUIRE_ESM` error.","wrong":"import { Server } from 'node-static';","symbol":"static.Server","correct":"const static = require('node-static');\nconst fileServer = new static.Server('./public');"},{"note":"The `serve` method takes `request` and `response` objects. Error handling is done via an optional callback or an 'error' event listener, not directly via a `next` middleware function as commonly found in frameworks like Express.","wrong":"fileServer.serve(request, response, next);","symbol":"fileServer.serve","correct":"fileServer.serve(request, response);"},{"note":"The path for `serveFile` should be relative to the server's root directory and typically starts with a leading slash. The headers object is a required argument, even if empty.","wrong":"fileServer.serveFile('error.html', 404, request, response);","symbol":"fileServer.serveFile","correct":"fileServer.serveFile('/error.html', 404, {}, request, response);"}],"quickstart":{"code":"var static = require('node-static');\nvar http = require('http');\n\n// Create a node-static server instance to serve the './public' folder.\n// Ensure a 'public' directory exists in your project root with some static files.\nvar file = new static.Server('./public', {\n    cache: 3600, // Cache files for 1 hour (default)\n    headers: {\n        'X-Powered-By': 'node-static'\n    }\n});\n\nhttp.createServer(function (request, response) {\n    // This pattern for handling request 'end' and 'resume' is for older Node.js versions.\n    request.addListener('end', function () {\n        file.serve(request, response, function (err, result) {\n            if (err) {\n                console.error(\"Error serving \" + request.url + \" - \" + err.message);\n                // Respond manually to the client if an error occurs\n                response.writeHead(err.status || 500, err.headers || { 'Content-Type': 'text/plain' });\n                response.end(\"Error serving file: \" + err.message);\n            }\n        });\n    }).resume(); // Important for older Node.js to ensure stream is read\n}).listen(8080, function() {\n    console.log('node-static server running on http://localhost:8080');\n    console.log('Serving files from ./public');\n});","lang":"javascript","description":"This quickstart sets up a basic HTTP server using `node-static` to serve files from a `./public` directory. It demonstrates the module's core `Server` instantiation, `serve` method, and basic error handling with an old Node.js `http` stream pattern."},"warnings":[{"fix":"This specific pattern is deeply integrated into `node-static`. For modern Node.js environments, it's recommended to use a contemporary static file serving library like `serve-static` or `express.static` that handles stream management correctly.","message":"Outdated Node.js `http` stream API usage: The `request.addListener('end').resume()` pattern is an older method for handling readable streams. In modern Node.js, request streams are typically flowing by default, and this pattern may cause unexpected behavior or hangs.","severity":"breaking","affected_versions":">=0.7.11 (when used with modern Node.js versions)"},{"fix":"Projects configured for ES Modules must either use `require()` (if compatible with their setup) or, preferably, migrate to an ESM-compatible static file server.","message":"Lack of ESM support: `node-static` is a CommonJS-only package. Attempting to use `import` statements in an ESM context will result in an `ERR_REQUIRE_ESM` error.","severity":"breaking","affected_versions":">=0.7.11 (in ESM projects)"},{"fix":"Avoid using `node-static` in new projects or existing projects requiring security patches. Migrate to actively maintained alternatives.","message":"Abandoned Status & Security Risks: The package has not been updated in over eight years. This means it likely contains unpatched security vulnerabilities and is not compatible with modern Node.js features or best practices, posing a significant security risk for production applications.","severity":"gotcha","affected_versions":">=0.7.11"},{"fix":"Implement robust, centralized error handling around `fileServer.serve` calls, or migrate to a framework that provides integrated error handling.","message":"Manual Error Handling: Unlike modern web frameworks that integrate error middleware, `node-static` requires explicit `response.writeHead` and `response.end` calls within its error callback. This increases boilerplate and potential for inconsistencies in error responses.","severity":"gotcha","affected_versions":">=0.7.11"},{"fix":"For any project requiring current Node.js support, it's strongly advised to use a different, actively maintained static file serving library.","message":"Outdated Node.js Compatibility: The package specifies Node.js `>= 0.4.1`, a version that reached End-of-Life over a decade ago. Using `node-static` with modern Node.js versions may lead to unexpected behavior due to API changes and deprecations.","severity":"deprecated","affected_versions":">=0.7.11 (when used with modern Node.js versions)"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Run `npm install node-static` in your project directory. Ensure `require('node-static')` is correctly spelled and located.","cause":"The package is not installed or the `require` path is incorrect.","error":"Error: Cannot find module 'node-static'"},{"fix":"This problem suggests fundamental incompatibility with modern Node.js stream handling. The most robust fix is to replace `node-static` with an actively maintained static file serving solution compatible with your Node.js version.","cause":"This indicates a mismatch between the expected `request` object API in `node-static` and the `http.IncomingMessage` object in newer Node.js versions, where `addListener` might not behave as expected with the `resume()` call.","error":"TypeError: request.addListener is not a function"},{"fix":"Ensure `const static = require('node-static');` is present and correctly scoped at the top of your file before `static.Server` is used.","cause":"The `static` variable was not properly assigned or is out of scope. This often happens if `require('node-static')` is omitted or placed incorrectly.","error":"ReferenceError: static is not defined"},{"fix":"If your project is ESM, you cannot directly `require` `node-static`. Consider migrating your project to CommonJS if `node-static` is critical, or, more practically, replace `node-static` with an ESM-compatible static file server.","cause":"You are attempting to use `require()` to load `node-static` in an ES Module context, or vice-versa with a modern dependency. `node-static` is CommonJS-only.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module [...] not supported."}],"ecosystem":"npm"}