Generate New Cloudflare Workers Projects with Wrangler's generate Command

Share this video with your friends

Social Share Links

Send Tweet

Wranglers generate command, wrangler generate, allows you to create new Cloudflare Workers projects. Using the URL parameter, you can also generate new projects based on Cloudflare's starter templates, or open-source projects created by other developers, but for now, you will just use the default. To do this you'll only need to give it a name, for example, wrangler generate my-worker, this will create a new project called my-worker.

Kristian Freeman: [0:00] Now that you've logged in with Wrangler, it's time to create your first project. To do this, we'll run the wrangler generate command. If I run wrangler generate -h, you can see I get some information about how to actually use this command including some flags, options, and arguments.

[0:16] Here's the basics of wrangler generator. First, you give it a name of your project, for instance, My Worker. Optionally, if you like, you can give it a URL which represents a template.

[0:27] In the Cloudflare Worker's documentation, you can find the starters page. It's just developers.cloudflare.com/workers/starters, which has a list of GitHub repos that you can use as templates. There's things like our default Hello World starter which is just pure JavaScript showing you how to return a response that's going to actually look a lot like what we're going to make throughout the rest of this course.

[0:50] Then, there's more advanced ones. It's like the Worker TypeScript template, which is a batteries included template for kick starting a TypeScript project and workers. There's also things like routers, built-in libraries for routing incoming requests to different functions. Even more advanced things, like a workers-graphql-server, which is a full on Apollo GraphQL server, built on top of Workers. There's all kinds of templates you can use.

[1:17] For now, we'll just use the default. We're not even going to pass in a template or a URL. We'll just say, wrangler generate my-worker. It's going to create a new project called, my-worker. You can see the text is a little faded there, but it just says, creating project called, my-worker. Then I have this new folder/home/kristian/src/my-worker.

[1:40] It gives me a little helpful hint here. You'll need to update the following fields in the created wrangler.toml file, specifically my account_id, which you can see here. For now, I'll just go into my-worker and I'll just look inside here. You can see there's not that much stuff that is add in, CODE_OF_CONDUCT, some licenses, a README.

[2:01] Then the three important files here. Index.js, which is of course, our actual JavaScript code. Package.json, which just indicates how the package works. Any NPM packages we want to add, we can specify here. Then, wrangler.toml, which is the actual Wrangler configuration file, which we'll dig deep into a little bit further in the course.

[2:24] If you're interested in all the other things you can do with Wrangler, you should definitely check out the Commands page which is also in our docs. You just come to the Workers docs, go to CLI (Wrangler) and check out Commands.

[2:36] You can see basically for every single command inside of Wrangler, the different arguments you can pass in, different flags, all different things that you can do here. There's a number of other Commands. Some of which we'll get to in this course. Others, you'll probably have to go check out yourself, or maybe I'll cover in a future lesson.