googleapis/google-cloud-node

The esm version of @google-cloud/tasks is incorrectly using `require` to load a JSON file

Closed this issue · 6 comments

Please make sure you have searched for information in the following guides.

A screenshot that you have tested with "Try this API".

n/a — a code issue with the esm version

Link to the code that reproduces this issue. A link to a public Github Repository or gist with a minimal reproduction.

https://gist.github.com/timomeh/1744248097efe05a3a6671e8c1af1ea8

A step-by-step description of how to reproduce the issue, based on the linked reproduction.

  1. Create an empty Next.js v14 app and install @google-cloud/tasks
  2. Copy the route handler from the gist and the next.config.js
  3. Run next build (ensure it's using the standalone output)
  4. Start the built app
  5. Try to call the route handler

A clear and concise description of what the bug is, and what you expected to happen.

Following the above steps, it should work and enqueue a task, but instead it errors with:

Error: Cannot find module '/app/node_modules/.pnpm/@google-cloud+tasks@5.4.0_encoding@0.1.13/node_modules/@google-cloud/tasks/build/esm/src/v2/cloud_tasks_client_config.json'

If you compare the contents of the directories /node_modules/@google-cloud/tasks/build/esm/src/v2/ with .next/standalone/node_modules/@google-cloud/tasks/build/esm/src/v2/, you can see that the standalone output is missing the cloud_tasks_client_config.json file.

A clear and concise description WHY you expect this behavior, i.e., was it a recent change, there is documentation that points to this behavior, etc. **

The esm package imports the file json-helper.cjs, which uses require() to load the file cloud_tasks_client_config.json. ESM can't use require().

Because of this, the standalone build of Next.js won't copy over the cloud_tasks_client_config.json file. Without this file, the SDK errors.

A quickfix is to not use the esm version, by loading the SDK with require('@google-cloud/tasks') instead of import

Issue was opened with an invalid reproduction link. Please make sure the repository is a valid, publicly-accessible github repository, and make sure the url is complete (example: https://github.com/googleapis/google-cloud-node)

What? I entered the reproduction link, it's a public gist, the issue template says "or gist"!

@sofisl If Gists aren't allowed as reproduction step, the issue template needs to be updated. It's not a great experience for someone to take the time and prepare an issue description, just for a bot to close it.

@timoneh thanks for the call-out, noticed a small issue with our regex that I've updated here: #5705. I've confirmed and it would have let your issue come through. That being said, this issue does look like this one, it seems to be an issue with the way Next copies over files. Can you try this workaround?

Sorry for not seeing the already opened issue #4858, I searched the issues for "cloud_tasks_client_config" and got no results, but I didn't search for "cloud_tasks_client_config.json". 🙄 My bad.

I also already tried the outputFileTracingIncludes config, like you posted in the linked workaround. But I also encountered the same issue as in your last comment here. I'm not using turborepo tho, I'm using nx.

I'll be watching #4858 then 🙂

@sofisl I've tried the workaround you've linked above with no luck.