N
Nuxt2mo ago
snow

None my my server APIs seem to be working #help

#❓・help #💬・discussion I built a fullstack nuxt app. I tested everything on my local and it works fine. All the APIs work fine. But when I deploy to cloudflare pages. And then test the app I get an error { "url": "/api/admin/members", "statusCode": 500, "statusMessage": "", "message": "__dirname is not defined", "stack": "" } When I test the API with the live url I get the same weird error also. Ironically, if I write a barebones API that does not but just returns a message or makes a DB query, it works fine. But anything more than this and I get the error
2 Replies
kapa.ai
kapa.ai2mo ago
Hi @snow. 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 information provided in the knowledge sources, it seems your issue is related to deploying a Nuxt application with API routes to Cloudflare Pages. Let me provide some insights and potential solutions: 1. API Routes Location: When using Nuxt with Cloudflare, you need to ensure your API routes are in the correct directory. According to the Nitro documentation (https://nitro.unjs.io/deploy/providers/vercel#api-routes), you should use the server/api/ directory for API routes when working with Nuxt. 2. Cloudflare Pages Configuration: Make sure you're using the correct preset for Cloudflare Pages deployment. The recommended preset is cloudflare_pages. You can set this in your Nitro configuration or as an environment variable during the build process. Here's how you can set it in your configuration:
// nuxt.config.js or nuxt.config.ts
export default defineNuxtConfig({
nitro: {
preset: 'cloudflare_pages'
}
})

// nuxt.config.js or nuxt.config.ts
export default defineNuxtConfig({
nitro: {
preset: 'cloudflare_pages'
}
})

Or as an environment variable:
NITRO_PRESET=cloudflare_pages npm run build

NITRO_PRESET=cloudflare_pages npm run build

3. __dirname Issue: The error message you're seeing (__dirname is not defined) suggests that you might be using Node.js-specific globals that are not available in the Cloudflare Workers environment. Cloudflare Workers run in a V8 isolate, which doesn't have access to Node.js APIs. You'll need to refactor any code that relies on __dirname or other Node.js-specific features. 4. Prerendering and API Routes: If you're using both static generation and API routes, you might need to configure Nitro to handle this correctly. Here's an example configuration that might help:
// nuxt.config.js or nuxt.config.ts
export default defineNuxtConfig({
nitro: {
prerender: {
crawlLinks: true,
routes: ['/'],
ignore: ["/api"]
}
}
})

// nuxt.config.js or nuxt.config.ts
export default defineNuxtConfig({
nitro: {
prerender: {
crawlLinks: true,
routes: ['/'],
ignore: ["/api"]
}
}
})

This configuration tells Nitro to prerender all routes except those under /api, which will be handled at runtime Stack Overflow (https://stackoverflow.com/questions/77292506). 5. Cloudflare Settings: Ensure that your Cloudflare settings are not interfering with your application. Disable features like Auto Minify, Rocket Loader, and Email Address Obfuscation as mentioned in the Nuxt deployment documentation (https://nuxt.com/docs/getting-started/deployment#cdn-proxy). If you've implemented these suggestions and are still facing issues, you might need to review your API code for any Node.js-specific features that are incompatible with Cloudflare Workers. Consider simplifying your API logic or using Cloudflare-compatible alternatives for any problematic operations. Remember to test your application locally using the Cloudflare Workers environment before deploying. You can use the wrangler (https://nitro.unjs.io/deploy/providers/cloudflare#wrangler) CLI tool for local testing and manual deployment. If the problem persists, you may need to provide more details about your API implementation or consider reporting a bug (https://nuxt.com/docs/community/reporting-bugs#figure-out-what-the-cause-might-be) to the Nuxt community, as there might be an underlying issue with the Nuxt-Cloudflare integration. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@655679745895170049> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Want results from more Discord servers?
Add your server