lambda-packages icon indicating copy to clipboard operation
lambda-packages copied to clipboard

Image and Picture components not building on production due to __dirname issue (docker-node)

Open josephfh opened this issue 3 years ago • 1 comments

What version of astro are you using?

1.2.6

Are you using an SSR adapter? If so, which one?

None

What package manager are you using?

pnpm

What operating system are you using?

Linux

Describe the Bug

The error:

Using Image or Picture components in an astro component cause the build to fail with __dirname is not defined in ES module scope

building client 
***snipped***
 generating static routes 
 error   __dirname is not defined in ES module scope
ReferenceError: __dirname is not defined in ES module scope
    at file:///app/dist/entry.mjs?time=1663653805836:5123:32
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:533:24)
    at async generatePages (file:///app/node_modules/.pnpm/[email protected]/node_modules/astro/dist/core/build/generate.js:70:20)
    at async staticBuild (file:///app/node_modules/.pnpm/[email protected]/node_modules/astro/dist/core/build/static-build.js:68:5)
    at async AstroBuilder.build (file:///app/node_modules/.pnpm/[email protected]/node_modules/astro/dist/core/build/index.js:86:5)
    at async AstroBuilder.run (file:///app/node_modules/.pnpm/[email protected]/node_modules/astro/dist/core/build/index.js:127:7)
    at async build (file:///app/node_modules/.pnpm/[email protected]/node_modules/astro/dist/core/build/index.js:22:3)
    at async runCommand (file:///app/node_modules/.pnpm/[email protected]/node_modules/astro/dist/cli/index.js:190:14)
    at async cli (file:///app/node_modules/.pnpm/[email protected]/node_modules/astro/dist/cli/index.js:207:5)
 ELIFECYCLE  Command failed with exit code 1.

The reason, maybe

  I'm not sure if I'm correct, but some googling tells me the error is related to to this:

In ESM, you might found your old friends __dirname and __filename are no longer available. https://antfu.me/posts/isomorphic-dirname

How to fix "__dirname is not defined in ES module scope" https://shramko.dev/blog/dirname-error

As Astro is made using "type": "module", on the docker-node image I'm using there is no system version of __dirname or __filename available. These should be added to the astro/image logic as mentioned in the links above

Why am I not providing to a Minimal Reproducible Example?

Sorry I can't reproduce the issue without my hosted build environment. My Dockerfile is provided as a Gist. Any use of an Image or Picture component should reproduce this bug.

I am happy to test any fix in my CI when available

Environment details:

Using Docker image node:16.17.0 and bullseye-slim https://github.com/nodejs/docker-node#nodebullseye

Dockerfile: https://gist.github.com/josephfh/411c3fbc9e09ac1515e6a453797b8ff5

Link to Minimal Reproducible Example

https://gist.github.com/josephfh/411c3fbc9e09ac1515e6a453797b8ff5

Participation

  • [ ] I am willing to submit a pull request for this issue.

josephfh avatar Sep 20 '22 06:09 josephfh