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

support environments without support for createRequire() & JSON modules #300

Open
AnsgarH1 opened this issue Oct 22, 2023 · 4 comments
Open

Comments

@AnsgarH1
Copy link

AnsgarH1 commented Oct 22, 2023

Hi there,
I just tried to deploy a Next.js app with React server components using hafas-client to Vercel.
It always worked as intended on my machine, but never when deployed to a lambda function as I always got a "could not import base.json" error.
I even went as far as forking the package and replacing the require statement with an import, but that resulted in a lambda timeout.
The solution was to use node 16 instead of node 18 in the Vercel config. I also changed the region from Washington D.C. to Frankfurt, but that probably won't make any difference.

I hope this info helped anyone, but it took me like 3 hours to troubleshoot.

Edit: i reverted back to using the official published npm package instead of my fork, but this broke it again and i got the " Cannot find module './base.json'" Error again.

@derhuerst
Copy link
Member

I don't think hafas-client needs to change here, so I can't really help here. Thanks for reporting your insights though!

I'm not very familiar with Vercel, but AFAIK this happens because it tries to bundle your hafas-client-code, including hafas-client and other dependencies, into a single file.

possibly related: vercel/ncc#578
possibly related: vercel/ncc#74
possibly related: vercel/ncc#548

@derhuerst derhuerst added question docs regarding documentation labels Nov 7, 2023
@derhuerst
Copy link
Member

What do you think about documenting this issue as known at least, e.g. in docs/vercel.md?

@derhuerst derhuerst changed the title Vercel Deployment not working (and how i solved it) Vercel deployment: workarounds for base.json not being found Nov 7, 2023
@mourabitiziyad
Copy link

I exported base.json data as a javascript object file in a fork of this package. This allowed the file (now base.js ) to be properly bundled on vercel. So, I was no longer getting the error. Maybe it wouldn't hurt to change the file format from json to js although we'll need to change all the references to use "import" instead, which I think is worth it as that's a better way of handling file imports.

@derhuerst

@derhuerst
Copy link
Member

Maybe it wouldn't hurt to change the file format from json to js although we'll need to change all the references to use "import" instead, which I think is worth it as that's a better way of handling file imports.

When switching to ECMAScript Modules (ESM) in todo, my assumption was that the Import Attributes Proposal, which would formalise JSON imports as they are already experimentally supported by Node.js, will be accepted soon, allowing us solve this without workarounds.

But by now I assume it will still take a while until we can use import attributes, so I'm fine with building a workaround.

I exported base.json data as a javascript object file in a fork of this package. This allowed the file (now base.js ) to be properly bundled on vercel. So, I was no longer getting the error.

You're welcome to submit a PR that modifies the pull-profile-base-data.sh script to generate JS ESM files instead which default-export the JSON.

@derhuerst derhuerst reopened this Jan 6, 2024
@derhuerst derhuerst changed the title Vercel deployment: workarounds for base.json not being found support environments without support for createRequire() & JSON modules Jan 6, 2024
@derhuerst derhuerst added enhancement help wanted and removed question docs regarding documentation labels Jan 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants