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

[solution] __dirname is not defined in ES module scope #2507

Closed
wojtekmaj opened this issue Jan 18, 2024 · 2 comments · Fixed by #2711
Closed

[solution] __dirname is not defined in ES module scope #2507

wojtekmaj opened this issue Jan 18, 2024 · 2 comments · Fixed by #2711
Labels

Comments

@wojtekmaj
Copy link
Contributor

wojtekmaj commented Jan 18, 2024

Describe the bug

When building react-pdf in ESM mode, you may encounter an error:

__dirname is not defined in ES module scope

This is because yoga-layout uses __dirname in their code. This has already been reported to them: facebook/yoga#1559

To work around this issue, for now, consider using inject option in esbuild:

cjs-shim.ts

import { createRequire } from 'node:module';
import path from 'node:path';
import url from 'node:url';

globalThis.require = createRequire(import.meta.url);
globalThis.__filename = url.fileURLToPath(import.meta.url);
globalThis.__dirname = path.dirname(__filename);

esbuild.ts

await esbuild.build({
  // …
  inject: ['cjs-shim.ts'],
  // …
});

If you're not using esbuild, you may manually import the cjs-shim.ts file BEFORE react-pdf is loaded.

@wojtekmaj
Copy link
Contributor Author

Added to official documentation in diegomura/react-pdf-site#128.

@ithustle
Copy link

@wojtekmaj, I'm having this issue using react-pdf on electron-vite. Could you tell more about how to workaround this issue?

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

Successfully merging a pull request may close this issue.

3 participants