Open zhengsihuaa opened 5 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?
wrangler.toml
file (or secrets, that should not be committed, in a .dev.vars
file)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 variableswrangler.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)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 π
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 pageI 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
@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?
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 {} trygetRequestContext().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?