Skip to content

Commit 4965aa4

Browse files
authored
docs(nodejs): clarify serveStatic root path resolution (#837)
* docs(nodejs): clarify serveStatic root path resolution and add import.meta.url exampleEnhance Node.js docs with path resolution guidance Add warning about path resolution in Node.js static serving * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Fixies honojs/hono#4442
1 parent 14f8924 commit 4965aa4

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

docs/getting-started/nodejs.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,23 @@ import { serveStatic } from '@hono/node-server/serve-static'
180180
app.use('/static/*', serveStatic({ root: './' }))
181181
```
182182

183+
::: warning
184+
The `root` option resolves paths relative to the current working directory (`process.cwd()`). This means the behavior depends on **where you run your Node.js process from**, not where your source file is located. If you start your server from a different directory, file resolution may fail.
185+
186+
For reliable path resolution that always points to the same directory as your source file, use `import.meta.url`:
187+
188+
```ts
189+
import { fileURLToPath } from 'node:url'
190+
import { serveStatic } from '@hono/node-server/serve-static'
191+
192+
app.use(
193+
'/static/*',
194+
serveStatic({ root: fileURLToPath(new URL('./', import.meta.url)) })
195+
)
196+
```
197+
198+
:::
199+
183200
Use the `path` option to serve `favicon.ico` in the directory root:
184201

185202
```ts

0 commit comments

Comments
 (0)