Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Failed to instantiate edge runtime.fig:load:flatten", "__dirname is not defined" #225

Closed
cheapsteak opened this issue Dec 16, 2022 · 5 comments

Comments

@cheapsteak
Copy link

cheapsteak commented Dec 16, 2022

Bug Report

Current behavior

Created a blank project to test middleware

Server starts up fine
> Success! Build completediming config:load:flatten Completed in 3ms
> Ready! Available at http://localhost:3000

but upon accessing localhost:3000, the page returns "This Edge Function has crashed", and the console logs

Failed to instantiate edge runtime.fig:load:flatten Completed in 3ms
__dirname is not defined

Expected behavior/code

The middleware function to run instead of returning page crash

Possible solution

Additional context/screenshots
Repro by running npm run ddev

Archive.zip

I don't believe this is a duplicate of #209 since I've tried running with node@16.19.0 and also downgrading to a version of vercel cli that was using edge-runtime@1.1.0 and received the same errors

@cheapsteak
Copy link
Author

looks like I was missing the step of adding next as a dependency 🤦
(I thought I didn't need it since I thought I saw somewhere that middleware and edge functions were framwork agnostic)

apologies for the noise

@cheapsteak
Copy link
Author

cheapsteak commented Dec 16, 2022

actually sorry this is now working locally with vercel dev but not working when deployed

function logs https://vercel.com/cheapsteak1/lskjdf/DTvY4XUMnwcJoGJU6vkq2rALiNcw/functions showing

[GET] /
13:56:48:09
ReferenceError: __dirname is not defined
    at worker.js:21:23819
    at worker.js:21:23861
    at worker.js:14:28
    at worker.js:21:24002
    at worker.js:14:28
    at worker.js:21:24667
    at worker.js:14:28
    at worker.js:21:25045
    at worker.js:14:28
    at worker.js:23:103

[GET] /favicon.ico
13:56:35:43
ReferenceError: __dirname is not defined
    at node_modules/next/dist/compiled/ua-parser-js/ua-parser.js:10:16756
    at node_modules/next/dist/compiled/ua-parser-js/ua-parser.js:10:16885
    at node_modules/next/dist/server/web/spec-extension/user-agent.js:8:41
    at node_modules/next/server.js:6:0
    at middleware.js:4:15
    at middleware:middleware.js:1:17

@cheapsteak cheapsteak reopened this Dec 16, 2022
@Kikobeats
Copy link
Member

Hello, the global __dirname is missing at Edge Runtime because it's a Node.js API and the Edge Runtime effort is to cover WinterCG API:

CleanShot 2023-01-18 at 10 23 27@2x

So it's expected to be missing there.

The thing is, if you do the same but using Next.js, __dirname exists:

CleanShot 2023-01-18 at 10 27 24@2x

That's because Next.js is extending Edge Runtime API surface, adding __dirname polyfill to be possible to cover more user cases.

@cheapsteak
Copy link
Author

Thanks for clarifying 🙏 does that mean edge-runtime is only meant to be used with NextJS projects?

@Kikobeats
Copy link
Member

Not really: You can extend Edge Runtime to add whatever you need:

const { EdgeRuntime } = require('edge-runtime')

const runtime = new EdgeRuntime({
  extend: context => {
    context.__dirname = __dirname
    return context
  }
})

Promise.resolve().then(async () => {
  const result = await runtime.evaluate('__dirname')
  console.log(result)
})

Take a look to https://edge-runtime.vercel.app/getting-started#usage 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants