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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[馃悰 Bug]: how to access env in api router? #760

Open
1 task
zhengsihuaa opened this issue Apr 28, 2024 · 1 comment
Open
1 task

[馃悰 Bug]: how to access env in api router? #760

zhengsihuaa opened this issue Apr 28, 2024 · 1 comment
Labels
question Further information is requested

Comments

@zhengsihuaa
Copy link

zhengsihuaa commented Apr 28, 2024

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?

  • Would you like to help fixing this bug?
@zhengsihuaa zhengsihuaa added the bug Something isn't working label Apr 28, 2024
@dario-piotrowicz
Copy link
Member

dario-piotrowicz commented May 8, 2024

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?

  • if you don't see them locally you need to set them in a wrangler.toml file (or secrets, that should not be committed, in a .dev.vars file)
  • if you don't see them in the deployed application:
    • if your wrangler.toml file is used for deployments (meaning that it has the pages_build_output_dir field) again you need to make sure that the toml file contains the variables
    • if no wrangler.toml file is used for deployments (i.e. you don't have a toml file or it you have one but it doesn't specify the pages_build_output_dir field) then you need to set the variables (or secrets) in the Cloudflare dashboard (secrets always need to be specified in the dashboard or handled via the wrangler pages secret command)

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 馃檪

@dario-piotrowicz dario-piotrowicz added question Further information is requested and removed bug Something isn't working labels May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants