cloudflare / next-on-pages

CLI to build and develop Next.js apps for Cloudflare Pages
https://www.npmjs.com/package/@cloudflare/next-on-pages
MIT License
1.25k stars 121 forks source link

[πŸ› Bug]: how to access env in api router? #760

Open zhengsihuaa opened 5 months ago

zhengsihuaa commented 5 months ago

next-on-pages environment related information

System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:41 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T8103 CPU: (8) arm64 Apple M1 Memory: 16 GB Shell: /bin/zsh Package Manager Used: pnpm (8.11.0)

Relevant Packages: @cloudflare/next-on-pages: 1.11.1 vercel: N/A next: 14.1.4

Description

I've migrated my Next.js from Vercel to Cloudflare, but now I have a serious issue. How can I access environment variables in the API router? On Vercel, I only needed to configure them in the project, and then I could access them via process.env. However, in Cloudflare, there are numerous methods (such as Environment, KV), and I've tried them all, but I still can't access the environment variables. I try const kvNamespace = (process.env as unknown as CloudflareEnv).DOMAIN_PRICE but got {} try getRequestContext().env still got {} does anyone can tell me how can I access env variable conveniently?

Reproduction

No response

Pages Deployment Method

Pages CI (GitHub/GitLab integration)

Pages Deployment ID

No response

Additional Information

No response

Would you like to help?

dario-piotrowicz commented 4 months ago

Hi @zhengsihuaa πŸ™‚πŸ‘‹

You should be able to access your variables by using the getRequestContext utility (and also with process.env although getRequestContext is the recommended way)

My guess here is that you're not setting up the variables correctly?

Where are you not seeing your variables? locally? or also in the deployed application? both?

As an example you can see https://github.com/dario-piotrowicz/next-on-pages-get-request-context-test-demo where the MY_VAR is set in the toml file and used in the server page

I hope the above helps! please let me know πŸ™‚

simplenotezy commented 3 months ago

An an example you can see https://github.com/dario-piotrowicz/next-on-pages-get-request-context-test-demo where the MY_VAR is set in the toml file and used in the server page

I hope the above helps! please let me know πŸ™‚

I don't see your toml configuration using the pages_build_output_dir. I am getting the following error when building:

β–² [WARNING] Pages now has wrangler.toml support.

  We detected a configuration file at /Users/mf/Projects/justfriendly/web/wrangler.toml but it is
  missing the "pages_build_output_dir" field, required by Pages.
  If you would like to use this configuration file to deploy your project, please use
  "pages_build_output_dir" to specify the directory of static files to upload.
  Ignoring configuration file for now, and proceeding with project deploy.

That also means that my d1 database cannot be deployed, because wrangler is not reading it

dario-piotrowicz commented 3 months ago

@simplenotezy you can set pages_build_output_dir to ".vercel/output/static" and that should do it πŸ™‚

PS: we removed that misleading warning from wrangler... I think... πŸ€”, are you on the latest wrangler version?