hashicorp / next-mdx-remote

Load MDX content from anywhere
Mozilla Public License 2.0
2.74k stars 141 forks source link

esbuild binary not found when using pnpm #129

Closed zaiste closed 3 years ago

zaiste commented 3 years ago

When using pnpm the esbuild binary cannot be located, i.e.

wait  - compiling...
event - compiled successfully
node:events:346
      throw er; // Unhandled 'error' event
      ^

Error: spawn /<my project>/node_modules/esbuild/bin/esbuild ENOENT
    at Process.ChildProcess._handle.onexit (node:internal/child_process:282:19)
    at onErrorNT (node:internal/child_process:480:16)
    at processTicksAndRejections (node:internal/process/task_queues:81:21)
Emitted 'error' event on ChildProcess instance at:
    at Process.ChildProcess._handle.onexit (node:internal/child_process:288:12)
    at onErrorNT (node:internal/child_process:480:16)
    at processTicksAndRejections (node:internal/process/task_queues:81:21) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'spawn /<my project>/node_modules/esbuild/bin/esbuild',
  path: '/<my project>/node_modules/esbuild/bin/esbuild',
  spawnargs: [ '--service=0.11.18', '--ping' ]
}
 ERROR  Command failed with exit code 1.

Adding esbuild directly to project's package.json solves the problem.

BRKalow commented 3 years ago

Thanks for the report! We're talking about ways to improve the esbuild binary detection amongst the team.

Would you be able to share a path to the binary without adding esbuild directly? That would be helpful!

Thanks!

zaiste commented 3 years ago

Thanks for your work on this fantastic package!

Right now the binary could be found at /node_modules/.pnpm/esbuild@0.11.18/node_modules/esbuild/bin, but there is a related discussion over at PNPM (cf. https://github.com/pnpm/pnpm/discussions/3191) to provide a more general support. It's not a big issue, this could wait till pnpm supports it.

kylegach commented 3 years ago

~I was able to workaround this by following related guidance from an alternative to this project: https://github.com/kentcdodds/mdx-bundler#nextjs-esbuild-enoent.~

~TL;DR: Add something like this before you call serialize:~

Snippet ```js if (process.platform === 'win32') { process.env.ESBUILD_BINARY_PATH = path.join( process.cwd(), 'node_modules', 'esbuild', 'esbuild.exe' ); } else { process.env.ESBUILD_BINARY_PATH = path.join( process.cwd(), 'node_modules', 'esbuild', 'bin', 'esbuild' ); } ```

Edit: Sorry, I didn't realize the nuances of the issue, so this workaround was only necessary for me because my Next.js app is located in a monorepo directory (my actual usage of the snippet above replaced process.cwd() with (effectively) process.cwd()/../..), so the baked in helper linked below doesn't quite work. I'm not actually using pnpm, and mistakenly thought the workarounds were related.

BRKalow commented 3 years ago

@kylegach We actually do have something like that baked in 😅 https://github.com/hashicorp/next-mdx-remote/blob/main/src/serialize.ts#L11-L38

shannonrothe commented 3 years ago

I am working in a monorepo and esbuild is installed in the top-level node_modules directory. It seems like the logic baked into next-mdx-remote only checks the most immediate node_modules directory (understandable). I wonder if there's any better solution than manually modifying that path to point to the right place.

BRKalow commented 3 years ago

@zaiste Could you try to upgrade to the latest version and see if that resolves your issue? We made some improvements to the esbuild binary resolution and I'm hoping it might help here as well.

zaiste commented 3 years ago

@BRKalow it works now without adding esbuild explicitly. Thank you!