👋 could somebody point me in the right

👋 could somebody point me in the right direction. I'm setting my container's env variables like this:
import { Container, getRandom } from "@cloudflare/containers";

const INSTANCE_COUNT = 3;

export class Backend extends Container {
defaultPort = 3001;
sleepAfter = "20m";
envVars = {
CF_ID: env.CF_ID,
};
import { Container, getRandom } from "@cloudflare/containers";

const INSTANCE_COUNT = 3;

export class Backend extends Container {
defaultPort = 3001;
sleepAfter = "20m";
envVars = {
CF_ID: env.CF_ID,
};
But then I get exception errors from the worker env is not defined
7 Replies
Mike Nomitch
Mike Nomitch•2mo ago
I'm guessing this is a copy/paste error, but there's a syntax error in the code Any chance that failed and then didn't deploy? missing } after envVars
jack
jackOP•2mo ago
Yeah paste error I think, here's the entire worker
import { Container, getRandom } from "@cloudflare/containers";

const INSTANCE_COUNT = 3;

export class Backend extends Container {
defaultPort = 3001;
sleepAfter = "20m";
envVars = {
CLOUDFLARE_ACCOUNT_ID: env.CLOUDFLARE_ACCOUNT_ID,
CLOUDFLARE_KV_NAMESPACE_ID: env.CLOUDFLARE_KV_NAMESPACE_ID,
CLOUDFLARE_API_TOKEN: env.CLOUDFLARE_API_TOKEN,
REDIRECT_URI: env.REDIRECT_URI,
SESSION_SECRET: env.SESSION_SECRET,
WDS_SOCKET_PORT: env.WDS_SOCKET_PORT,
NODE_ENV: env.NODE_ENV,
PORT: env.PORT,
};

// Optional lifecycle hooks
onStart() {
console.log("Backend container successfully started");
}

onStop() {
console.log("Backend container successfully shut down");
}

onError(error) {
console.log("Backend container error:", error);
}
}

export default {
async fetch(request, env) {
const url = new URL(request.url);

// Route API requests to the backend container
if (url.pathname.startsWith("/api")) {
// note: "getRandom" to be replaced with latency-aware routing in the near future
const containerInstance = await getRandom(env.BACKEND, INSTANCE_COUNT);
return containerInstance.fetch(request);
}

// Serve static assets for all other requests
return env.ASSETS.fetch(request);
},
};
import { Container, getRandom } from "@cloudflare/containers";

const INSTANCE_COUNT = 3;

export class Backend extends Container {
defaultPort = 3001;
sleepAfter = "20m";
envVars = {
CLOUDFLARE_ACCOUNT_ID: env.CLOUDFLARE_ACCOUNT_ID,
CLOUDFLARE_KV_NAMESPACE_ID: env.CLOUDFLARE_KV_NAMESPACE_ID,
CLOUDFLARE_API_TOKEN: env.CLOUDFLARE_API_TOKEN,
REDIRECT_URI: env.REDIRECT_URI,
SESSION_SECRET: env.SESSION_SECRET,
WDS_SOCKET_PORT: env.WDS_SOCKET_PORT,
NODE_ENV: env.NODE_ENV,
PORT: env.PORT,
};

// Optional lifecycle hooks
onStart() {
console.log("Backend container successfully started");
}

onStop() {
console.log("Backend container successfully shut down");
}

onError(error) {
console.log("Backend container error:", error);
}
}

export default {
async fetch(request, env) {
const url = new URL(request.url);

// Route API requests to the backend container
if (url.pathname.startsWith("/api")) {
// note: "getRandom" to be replaced with latency-aware routing in the near future
const containerInstance = await getRandom(env.BACKEND, INSTANCE_COUNT);
return containerInstance.fetch(request);
}

// Serve static assets for all other requests
return env.ASSETS.fetch(request);
},
};
Beny
Beny•2mo ago
Should be this.env ?
jack
jackOP•2mo ago
this.env worked thank you I couldn't spot that in any of the docs FYI @Mike Nomitch
Beny
Beny•2mo ago
Yeah docs are incorrect it seems Also MAX_ALAEM_RETRIES is spelt wrong in Container class 😂
Mike Nomitch
Mike Nomitch•2mo ago
Oh sorry @jack - you can import env at the top level not on this, so I used that in the docs example but then didnt include the import
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?