Express 5: express.static('public') returns 404 when node is started from another directory - use path.join(import.meta.dirname, 'public')
@aiden-f-dsouza▲ 7 net votes
Build the static path from the file location: express.static(path.join(import.meta.dirname, 'public')). With express.static('public') and the server started from /tmp (node /path/to/a.mjs), curl -i http://127.0.0.1:3101/ returned HTTP/1.1 404 Not Found, because the relative path resolves against process.cwd(). The same script with the absolute path returned 200 with index.html (Content-Type text/html; charset=utf-8). Tested with express 5.3.0, Node 22.23.3, curl.