N
Nuxt13h ago
losfroger

Bun APIs in nuxt server APIs

Does anyone know a way to use the Bun APIs like Bun.file inside Nuxt's server APIs? In my nuxt config I already set
nitro: {
preset: 'bun',
},
nitro: {
preset: 'bun',
},
But I still get an error: "message": "Bun is not defined", when trying to use Bun.file I also tried importing it directly like so:
import { file } from 'bun'

export default defineEventHandler(async (event) => {
const fooFile = file('./test.txt')
return await fooFile.text()
})
import { file } from 'bun'

export default defineEventHandler(async (event) => {
const fooFile = file('./test.txt')
return await fooFile.text()
})
But I get the following error: "message": "Cannot find package 'bun' imported from ...
2 Replies
kapa.ai
kapa.ai13h ago
Hi @losfroger. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the available knowledge sources, there is no official documentation or guidance confirming that you can directly use Bun's native APIs (like Bun.file) inside Nuxt server APIs, even when setting nitro.preset: 'bun' in your nuxt.config.ts. The error "Bun is not defined" or "Cannot find package 'bun'" suggests that the Bun global or its APIs are not exposed to your server code in the way you might expect. The documentation does mention that Nuxt (via Nitro) supports the Bun runtime as a deployment target, and you can set the preset accordingly:
export default defineNuxtConfig({
nitro: {
preset: 'bun',
}
})
export default defineNuxtConfig({
nitro: {
preset: 'bun',
}
})
However, it does not state that Bun's global APIs are available in your server handlers, nor does it provide examples of using Bun-specific features like Bun.file in server routes or middleware. The focus is on universal compatibility and deployment, not on exposing Bun-specific APIs to your application code docs: improve development documentation for using the Bun runtime. Additionally, attempting to import from 'bun' fails because there is no published npm package named 'bun'—the Bun runtime provides its APIs globally, but only in certain contexts, and Nuxt's server code may not run in that context. Summary:
The knowledge sources do not contain enough information to confirm that Bun APIs like Bun.file are supported or accessible inside Nuxt server APIs, even when using the Bun preset. If you require Bun-specific features, you may need to look for updates in the Nitro or Nuxt documentation or consider using standard Node.js APIs for file access, which are supported across all Nitro runtimes Server > Advanced Usage. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai13h ago
<@226402225621041152> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?