image icon indicating copy to clipboard operation
image copied to clipboard

Module causes "__dirname is not defined" error when deploying to Cloudflare Workers

Open MattFaz opened this issue 2 years ago • 3 comments

Nuxt3 documents advise you can deploy to Cloudflare Workers. Guide can be found on the Nitro documentation.

The commands wrangler dev and wrangler publish work successfully when @nuxt/image-edge is commented out of the nuxt.config.ts file. However when adding adding it back I receive the following error:

Error: Something went wrong with the request to Cloudflare...
Uncaught ReferenceError: __dirname is not defined
  at worker.js:1:918893
  at worker.js:1:1051834
 [API code: 10021]

Video attached showing it working with @nuxt/image-edge commented out and then adding it back resulting in the error.

https://user-images.githubusercontent.com/10377148/187341690-8c20f653-de1f-4c1a-af90-a0f478850320.mov

MattFaz avatar Aug 30 '22 03:08 MattFaz

This probably has something to do with static site generation still not supported.

ricky11 avatar Sep 15 '22 07:09 ricky11

Same thing happened to me :disappointed:

398noe avatar Sep 23 '22 17:09 398noe

any updates?

pxtrez avatar Oct 13 '22 20:10 pxtrez

Same, this completely prevents me from using the module.

BlakeB415 avatar Oct 18 '22 20:10 BlakeB415

Experiencing the same thing.

OlaAlsaker avatar Dec 07 '22 23:12 OlaAlsaker

I also am experiencing this issue

rvmourik avatar Jan 03 '23 08:01 rvmourik

Also just experienced this

dolbex avatar Jan 05 '23 22:01 dolbex

Same here!

vilhelmjosander avatar Jan 27 '23 07:01 vilhelmjosander

It seems that when cloudflare is selected as the provider the link is: /cdn-cgi/image/f=webp/<image_name>" while it should be: https://imagedelivery.net/<zone_id>/<image_id>/<variant_name>

cliqer avatar Jan 28 '23 01:01 cliqer

Same here, cannot deploy to CF workers

oleghalin avatar Jan 30 '23 09:01 oleghalin

Same problem here.

You can use this repo to reproduce.

  1. Clone https://github.com/brpaz/nuxt-starter
  2. Add @nuxt/image-edge module to nuxt.config.ts
  3. Run yarn install && yarn build:cloudflare
  4. Run npx wrangler dev

brpaz avatar Feb 04 '23 15:02 brpaz

This has been fixed on the latest update of @nuxt/image-edge. You simply need to enable image optimisation in Cloudflare. Your domain -> Speed -> Optimization -> Image Resizing

cliqer avatar Feb 04 '23 15:02 cliqer

@cliqer Thanks for answering, but Image Resizing is a Pro Cloudflare feature. I only want to deploy my site to Cloudflare and not have it deal with images.

I think I misunderstood how nuxt/image is supposed to work. I thought the optimized images would be generated at build time, but after checking IPX, this doesn´t seem to be the case and behaves as a server-side proxy to do the optimizations.

Is there any way I can use nuxt/image, only at build time and not rely on a Server?


Edit: Think I found my answer. Need to use the static provider. I don´t have the error anymore.

Add the following to the nuxt.config.ts

  image: {
    provider: 'static',
  },

brpaz avatar Feb 04 '23 16:02 brpaz

@brpaz I tried on the latest version and using the static provider as you suggested but I get this error Unknown provider: static. Did you run into this?

TaylorFinklea avatar Feb 17 '23 13:02 TaylorFinklea

@brpaz I tried on the latest version and using the static provider as you suggested but I get this error Unknown provider: static. Did you run into this?

Yup I just ran into the same. Any word on why this happens?

AdiRishi avatar Mar 10 '23 07:03 AdiRishi

Any update on this one? I'm also getting the following error when trying to deploy it to CloudFlare workers:

ReferenceError: __dirname is not defined
    at <PROJECT_DIR>node_modules/sharp/lib/libvips.js:23:30
    at <PROJECT_DIR>.nuxt/dist/server/_nuxt/Footer-styles-1.mjs-1e6a6f56.js:1:1
    at /private/var/folders/jn/f7gcf06507348j3qq0497h180000gp/T/tmp-3668-Tp4HcaifcnBG/index.js:13931:3
    at Script.runInContext (node:vm:141:12)
    at VMScriptRunner.runAsScript (<USER_DIR>.nvm/versions/node/v16.17.0/lib/node_modules/wrangler/node_modules/@miniflare/runner-vm/src/index.ts:26:12)
    at VMScriptRunner.run (<USER_DIR>.nvm/versions/node/v16.17.0/lib/node_modules/wrangler/node_modules/@miniflare/runner-vm/src/index.ts:88:12)
    at EventTarget.#reload (<USER_DIR>.nvm/versions/node/v16.17.0/lib/node_modules/wrangler/node_modules/@miniflare/core/src/index.ts:790:42)
    at EventTarget.getPlugins (<USER_DIR>.nvm/versions/node/v16.17.0/lib/node_modules/wrangler/node_modules/@miniflare/core/src/index.ts:1033:5)
    at createServer (<USER_DIR>.nvm/versions/node/v16.17.0/lib/node_modules/wrangler/node_modules/@miniflare/http-server/src/index.ts:362:19)
    at startServer (<USER_DIR>.nvm/versions/node/v16.17.0/lib/node_modules/wrangler/node_modules/@miniflare/http-server/src/index.ts:469:18)
Waiting for the debugger to disconnect...

I tried also the Cloudflare Pages deployment, but didn't get one working with the nitro_preset, if that's the better method I would like to have some help with that one.

jkneepkens avatar Mar 10 '23 10:03 jkneepkens

Getting same ReferenceError: __dirname is not defined error

ndragun92 avatar Mar 10 '23 13:03 ndragun92

I'm encountering the same error:

Uncaught ReferenceError: __dirname is not defined
    at worker.js:5680:7030
    at worker.js:17999:4
    at worker.js:18000:3
   [code: 10021]

In nuxt.config.ts, I have set:

image: {
    prismic: {},
  },

Curiously, the error only occurs for our automatic CI deployment via Github actions. Manual deployment from my machine works.

shuesken avatar Mar 16 '23 12:03 shuesken

It also happens with NITRO_PRESET=deno

fro-profesional avatar Mar 18 '23 17:03 fro-profesional

Getting same error "ReferenceError: __dirname is not defined"

duncandejong avatar May 22 '23 15:05 duncandejong

Same here: Error: Failed to publish your Function. Got error: Uncaught ReferenceError: __dirname is not defined. Is there any workaround or fix for this? Seems like the issue has existed for a while now. I wonder if anyone found a solution...

Aietes avatar May 22 '23 16:05 Aietes

Same here: Error: Failed to publish your Function. Got error: Uncaught ReferenceError: __dirname is not defined. Is there any workaround or fix for this? Seems like the issue has existed for a while now. I wonder if anyone found a solution...

I made local copies of the images for now (small site), cant use this module until fixed.  ❤️ to the developers! If i could help... i would. I am not that technical

duncandejong avatar May 22 '23 16:05 duncandejong

not sure if this helps, but was mentioned in the nuxt discord:

"__dirname is something Node provides, but cloudflare is using its own JS runtime which doesn't provide __dirname https://developers.cloudflare.com/workers/runtime-apis/"

mattgrah-am avatar May 24 '23 00:05 mattgrah-am

This has been fixed on the latest update of @nuxt/image-edge. You simply need to enable image optimisation in Cloudflare. Your domain -> Speed -> Optimization -> Image Resizing

Definitely not fixed. I have this enabled and still the exact same issue...

digitdash avatar Jun 04 '23 00:06 digitdash

This was resolved in the first RC with https://github.com/nuxt/image/pull/840 although until the next release, you will need to add the following line to your nuxt.config:

export default defineNuxtConfig({
  image: {
    none: {}
  }
})

danielroe avatar Jun 08 '23 13:06 danielroe