'local development with secrets
I am following this guide to get secrets added to my prod environment with cloudflare workers:
https://developers.cloudflare.com/workers/platform/environment-variables/#comparing-secrets-and-environment-variables
I am able to add new secrets via wrangler secret put
, and I see them in the dashboard. When I run my code locally with wrangler, it doesn't look like the variables are injected. I'm getting an error like this:
Uncaught ReferenceError: TOKEN is not defined
at line 0
at throwFetchError (/Users/justin.beckwith/.nvm/versions/node/v16.14.0/lib/node_modules/wrangler/wrangler-dist/cli.js:134316:17)
at fetchResult (/Users/justin.beckwith/.nvm/versions/node/v16.14.0/lib/node_modules/wrangler/wrangler-dist/cli.js:134287:5)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async previewToken (/Users/justin.beckwith/.nvm/versions/node/v16.14.0/lib/node_modules/wrangler/wrangler-dist/cli.js:134658:29)
at async createWorker (/Users/justin.beckwith/.nvm/versions/node/v16.14.0/lib/node_modules/wrangler/wrangler-dist/cli.js:134675:17)
at async start (/Users/justin.beckwith/.nvm/versions/node/v16.14.0/lib/node_modules/wrangler/wrangler-dist/cli.js:136075:16) {
I know the secret is set, and from what I can tell the values should be auto-injected. Any ideas on what I'm missing here? Thank you!
Solution 1:[1]
If you are using Module Workers you should be able to access your secrets via env
.
export default {
async fetch(request, env, context) {
return new Response(`TOKEN: ${env.TOKEN}`)
}
}
I am currently using wrangler2 version 0.0.17
Solution 2:[2]
To expand on melops answer above in Typescript:
interface Env {
var_name: var_type
}
export default {
async fetch(request: Request, env: Env, context: ExecutionContext): Promise<Response> {
console.log(env);
},
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | melops |
Solution 2 | Bram Adams |