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

Open
wojtekmaj opened this issue Jan 18, 2024 · 1 comment · May be fixed by #2711
Open

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

wojtekmaj opened this issue Jan 18, 2024 · 1 comment · May be 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.

wojtekmaj added a commit to wojtekmaj/diegomura-react-pdf that referenced this issue Apr 10, 2024
Closes diegomura#2692
Closes diegomura#2507

This PR updates Yoga to 3.0.

See the list of new features, improvements, and potential breaking changes in diegomura#2692.
@wojtekmaj wojtekmaj linked a pull request Apr 10, 2024 that will close this issue
wojtekmaj added a commit to wojtekmaj/diegomura-react-pdf that referenced this issue Apr 10, 2024
Closes diegomura#2692
Closes diegomura#2507

This PR updates Yoga to 3.0.

See the list of new features, improvements, and potential breaking changes in diegomura#2692.
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.

2 participants