Create a New Cloudflare Workers Project with Wrangler

Share this video with your friends

Send Tweet
Published 4 months ago
Updated 3 weeks ago

Creating a new workers project using Wrangler is pretty easy. We first will install and double-check that we have Wrangler installed. Then we will connect Wrangler to our cloudflare project. Lastly, using the terminal, we can easily create our new Wrangler project.

Instructor: [0:00] Let's begin by installing Cloudflare CLI, Wrangler. To do that, I'll run npm install -g for a global install, and then the package name, cloudflare/wrangler. You can check that you have Wrangler installed by running wrangler --version. Right now, I'm at Wrangler 1.19.3, though any version of Wrangler will work.

[0:21] Next, we'll connect Wrangler to our Cloudflare account. To do that, I'll run Wrangler login. Wrangler will ask if it's OK to open a page in my browser, I'll say y for yes. In the browser, you'll see this authorization page that says, "Allow Wrangler to make changes to your Cloudflare account." You can see all the different permissions it's going to ask for, and I'll just say, "Allow."

[0:43] Now, I look at this page, "You have granted authorization to Wrangler. Welcome to serverless execution environment," etc. We're now logged in. Feel free to close this browser window. Now, back here in the console, you can see it says, "Successfully configured. You can find your config file at this path." We know that we're now authorized.

[1:00] Next, let's create a new project which is what we're going to be using to learn more about Workers KV. To do that, I'll run Wrangler Generate and then I'll give it a project name like My KV Project.

[1:13] Wrangler will create a new project in the folder, My KV Project. Now let's open that up and take a look. You can see that we've created this new project here with all of these files. The ones you need to know are name, which is My KV Project.

[1:25] This is going to be the name that your project is deployed to when it's deployed to Cloudflare wWorkers. Type, which is just JavaScript. You can customize this to make use of some different build systems, or you can provide your own.

[1:36] Then account ID and workers dev, route, Zone ID, all relate to how and where you deploy your application. If you want to deploy to a custom domain, or you want to deploy between different accounts, you can do all of that here.

[1:49] Finally, compatibility date is a tool you can use to opt into different features in the Wranglers runtime, for instance, bug fixes and patches, and stuff like that. Though for now we're just going to leave all of this as is. We don't need to configure anything extra at the moment.

[2:03] Index.js is our source code, which we'll be filling in with more functionality. Though for now you just need to look at the basics here and see that we have this fetch event coming in.

[2:13] It responds to that fetch event using this handle requests function, which just returns a new response with the text, "Hello Worker." This is just a content type of text plain. It's literally just plain text being returned back to the client, though we'll customize this more.

[2:29] If you want to learn more about how that works, check out my, "Getting Started with Cloudflare Workers" course.