How can I import a utility function from the utils directory into my edge function in Nuxt3?
If I have a function sum in /utils/sum.js
export const sum = (…nums) => nums.reduce((prev, current) => prev + current)
And an edge function in netlify/edge-functions/foo.js
const result = sum(1, 2, 3)
export default () => {
return Response.json({ sum: result })
}
export const config = {
path: '/foo'
}
This breaks and sum is not defined. I am able to import bare modules into the edge function but I can‘t import my own utility functions.
I am also running netlify dev from the terminal since starting nuxt with npm run dev will not load the functions.
In Nuxt 3,
utilsis a folder that is scanned and has auto imports feature by default. If you are usingnetlify edge functionsyou have to be careful how you import the utility function into your edge function file and it will work just fine.Note that at the project root we have this folder structure:
Below are three examples:
Direct Import
Dynamic Import
Import Map
deno.jsonfile.netlify/edge-functions/foo.jsLastly run
netlify devand you can test the edge functions.