Skip to content

Wrangler CLI -- Deploy and Manage Workers

DodaTech 4 min read

In this tutorial, you'll learn about Wrangler CLI. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

Wrangler is the official command-line interface for Cloudflare Workers that lets you scaffold projects, run a local development server, and deploy your code to Cloudflare's global edge network using simple commands.

Why Wrangler CLI Matters

Manual uploads through the Cloudflare dashboard are impractical for real development workflows. Wrangler automates build, Bundling, environment management, secret handling, and deployment. It integrates with your existing tools -- git, CI/CD pipelines, and text editors -- so you can treat Workers code like any other software project. Paired with Cloudflare, Wrangler is the bridge between your local editor and production edge deployment.

Real-world use: A team of four developers collaborates on an API Gateway Worker. They use Wrangler's --env flag to maintain separate staging and production deployments, each with its own KV namespace bindings and secrets. Wrangler deploys in under 5 seconds per environment.

Development Workflow

flowchart LR
    I[wrangler init] --> D[wrangler dev]
    D --> T[Local dev server]
    T --> P[wrangler deploy]
    P --> E[Edge production]
    D --> S[wrangler tail]
    style I fill:#f90,color:#fff
    style D fill:#f90,color:#fff
    style P fill:#f90,color:#fff

Installing and Initializing

Install Wrangler globally via npm and create a new project.

npm install -g wrangler
wrangler init my-first-worker
cd my-first-worker

Expected output: Wrangler creates a directory with src/index.js (the Worker entry point), wrangler.toml (configuration), and package.json. The wrangler.toml contains your account ID and project settings.

Local Development with wrangler dev

The wrangler dev command starts a local HTTP server that emulates the Workers runtime.

export default {
  async fetch(request) {
    const url = new URL(request.url);
    if (url.pathname === '/api/time') {
      return new Response(String(Date.now()));
    }
    return new Response('Try /api/time');
  }
}
wrangler dev
# Starting local server at http://localhost:8787

Expected output: Running curl http://localhost:8787/api/time returns a Unix timestamp. The local dev server supports hot reloading -- save the file and the server restarts automatically.

Deploying to Production

Deploy your Worker to Cloudflare's edge with a single command.

wrangler deploy

Expected output: The terminal shows Uploaded and Published messages with the deployment URL, such as https://my-first-worker.example.workers.dev. The Worker is live at all 330+ Cloudflare data centers within seconds.

Managing Environment Variables and Secrets

Set environment-specific variables and encrypted secrets using Wrangler.

wrangler secret put API_KEY
# Enter the secret value: ********
wrangler deploy --env production
export default {
  async fetch(request) {
    const apiKey = env.API_KEY;
    const response = await fetch('https://api.example.com/data', {
      headers: { 'Authorization': `Bearer ${apiKey}` }
    });
    return response;
  }
}

Expected output: wrangler secret put stores the value encrypted and makes it available as env.API_KEY at runtime. Secrets are never readable after creation -- they can only be replaced or deleted.

Common Errors

Error Cause Fix
You must be logged in No authentication token found Run wrangler login or set <a href="/web-servers-hosting/cloudflare/">Cloudflare</a>_API_TOKEN
Invalid account ID Wrong account ID in wrangler.toml Run wrangler whoami to verify your account
Route already defined Duplicate route in wrangler.toml Remove duplicate route entries in routes array
Script size exceeded Worker bundle too large over 1MB Reduce dependencies, use wrangler deploy --dry-run to check size
Missing binding Referencing a binding not in wrangler.toml Add kv_namespaces or vars section for the binding

Practice Questions

  1. What command starts a local development server for a Cloudflare Worker?
  2. How do you deploy a Worker to a specific environment like staging or production?
  3. What is the difference between vars in wrangler.toml and wrangler secret put?

FAQ

Can I use Wrangler with CI/CD pipelines?

Yes, Wrangler supports non-interactive authentication using the <a href="/web-servers-hosting/cloudflare/">Cloudflare</a>_API_TOKEN environment variable, making it suitable for GitHub Actions, GitLab CI, and other CI/CD systems.

{{< faq "Does Wrangler support TypeScript?">}} Yes, Wrangler includes built-in TypeScript support. Running wrangler init --<a href="/programming-languages/typescript/">TypeScript</a> generates a project with TypeScript configuration and type definitions for the Workers runtime APIs. {{< /faq >}}

Summary

Wrangler CLI gives you the full developer workflow for Cloudflare Workers: project scaffolding with wrangler init, local testing with wrangler dev, and production deployment with wrangler deploy. Secret management, multi-environment support, and CI/CD integration make it production-ready. This tool is used by Doda Browser's engineering team to deploy privacy proxy Workers across staging and production environments. Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro -- security-first tools for the modern web.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro