environment variable
I defined a Environment Variables on the dashboard
Variable name = SERVER_URL, Value = server.xxx.com
In my code I use const serverURL = env.SERVER_URL or ENV.SEREVER_URL to call this variable that was defined on the dashboard but the console will show env, ENV is not defined.
Then I try using const serverURL = process.env.SERVER_URL, console will show Server URL : undefined.
How can I call SERVER_URL that was defined on the dashboard?
5 Replies
env is defined on the ContextObject of a Function
Could you give me a document or example of how to call SERVER_URL ?
Also if you defined in the dashboard, they won’t be available in local mode with Pages.
See https://developers.cloudflare.com/pages/platform/functions/bindings/#interact-with-your-environment-variables-locally for using them locally
Bindings · Cloudflare Pages docs
A binding enables your Pages Functions to interact with resources on the Cloudflare developer platform. Use bindings to integrate your Pages Functions …
I'm not clearly understand after reading a document.
Can I use const serverURL = context.env.SERVER_URL ?
I tried using this code
export function onRequest(context) {
if (context.env.SERVER_URL === 'development') {
return new Response('This is a local environment!');
} else {
console.log('serverURL', SERVER_URL);
return new Response(SERVER_URL);
}
}
but the result was Uncaught TypeError: Cannot read properties of undefined (reading 'env')