Dynamic Imports

Load environment-bound modules inside Cloudflare Workers request handlers.

Updated Aug 27, 2026
Ready to run
npx skills add https://github.com/captjay98/livestockai --skill dynamic-imports
💬 Prompt for ChatGPT / Claude Chat▼
Please help me install this Agent Skill.
Skill: Dynamic Imports
Source: https://github.com/captjay98/livestockai/tree/main/.kiro/skills/dynamic-imports
Command: npx skills add https://github.com/captjay98/livestockai --skill dynamic-imports

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Cloudflare Workers do not expose process.env at module load time. Environment variables are only accessible via the env binding during request handling. This skill explains how to use dynamic imports inside request handlers to access runtime-only modules and environment details safely.

Core Features & Use Cases

  • Dynamic imports inside server function handlers to access environment-bound modules and avoid module-load-time failures.
  • Singleton pattern for database access via getDb(), ensuring a single connection per request lifecycle.
  • Examples of patterns for authentication middleware, farm access checks, and database operations to maintain compatibility on Cloudflare Workers.

Quick Start

Place dynamic imports inside your request handler and use getDb() to obtain the database connection.

Frequently Asked Questions about Dynamic Imports

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I access environment variables in Cloudflare Workers when they are undefined at module load time?▼

To access environment variables in Cloudflare Workers, use dynamic imports inside request handlers. This pattern defers loading modules until request time, allowing your server functions to safely access env bindings and runtime-only resources.

Why does process.env fail on Cloudflare Workers server functions?▼

Process.env fails because Cloudflare Workers do not expose it at module load time. Environment variables are only accessible via the env binding during request handling, making dynamic imports necessary to load environment-bound modules at runtime.

What is the best way to manage database access in Cloudflare Workers request handlers?▼

The best way to manage database access is using a getDb() singleton helper with dynamic imports. This pattern ensures a single database connection per request lifecycle while maintaining compatibility with Cloudflare Workers env bindings.

Can I use dynamic imports for authentication middleware on Cloudflare Workers?▼

Yes, you can use dynamic imports for authentication middleware on Cloudflare Workers. This approach loads authentication and utility check modules at request time, ensuring secure access to environment-bound resources within request handlers.

Does Cloudflare Workers support dynamic imports for environment-bound modules?▼

Cloudflare Workers supports dynamic imports for environment-bound modules when placed inside request handlers. This ensures server functions access runtime-only modules and environment details safely without causing module-load-time failures.

What are the limitations of loading modules at module load time on Cloudflare Workers?▼

Loading modules at module load time on Cloudflare Workers fails because environment variables are only accessible via the env binding during request handling. You must use dynamic imports inside request handlers to avoid these runtime environment access limitations.