question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

defineEventHandler() is undefined in ~/server/api on Stackblitz

See original GitHub issue

Environment

Stackblitz starter template


Nuxt CLI v3.0.0-rc.2 (and Nuxt CLI v3.0.0-rc.1) RootDir: /home/projects/nuxt-starter-sh8cyl 10:06:48 Nuxt project info: 10:06:48


  • Operating System: Linux
  • Node Version: v16.14.2
  • Nuxt Version: 3.0.0-rc.2
  • Package Manager: npm@7.17.0
  • Builder: vite
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/nuxt-starter-sh8cyl?file=server/api/hello.ts

Describe the bug

The defineEventHandler() function used in the ~/server/api folder is marked as undefined.

Error message: Cannot find name 'defineEventHandler'.(2304) any

Additional context

As stated in the documentation,

Nuxt exposes every auto-import with the #imports alias that can be used to make the import explicit if needed

import { defineEventHandler } from '#import'; returns an error too: Cannot find module '#import' or its corresponding type declarations.

Logs

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
mumumilkcommented, May 26, 2022

managed to get it working by adding h3 as a dependency and calling it explicitly:

in server/api/middleware/log.js

import { defineEventHandler } from 'h3';

export default defineEventHandler((ev) => console.log(ev.req.url));
3reactions
danielroecommented, May 6, 2022

I believe this is caused by the fact that stackblitz hides the .nuxt folder. Let’s see what the team there might be able to do.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nuxt 3 - issue #4290 - StackBlitz
export default defineEventHandler(async () =>. {. return config;. }); Enter to Rename, Shift+Enter to Preview. Terminal.
Read more >
Using Firebase and Nuxt3 server routes error: "i.consume is ...
So this is a problem with stackblitz as the same exact code works fine ... using function inside defineEventHandler() in server/api/ file.
Read more >
useCookie · Nuxt Composables
You can use getCookie and setCookie from h3 package to set cookies in server API routes. Example: export default defineEventHandler(event => { //...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found