Debug Workers
Debugging is a critical part of developing a new application — whether running code in the initial stages of development, or trying to understand an issue occurring in production. In this article, you will learn how to effectively debug your Workers application, as well as review some code samples to help you get started:
Local testing with wrangler dev
The wrangler dev
command starts a local server for developing your Worker. wrangler dev
allows you to get feedback quickly while iterating by exposing logs on localhost
and to experiment without deploying to production. wrangler dev
can significantly reduce the time it takes to test and debug new features.
wrangler dev
will run the preview of the Worker directly on your local machine using the open source Cloudflare Workers runtime,
workerd and the Miniflare simulator.
In addition to testing Workers locally with wrangler dev
, the use of Miniflare allows you to test other Developer Platform products, such as R2, KV, D1, and Durable Objects.
Local testing against remote resources
wrangler dev
runs locally by default. To deploy your application to the edge preview service, and make it available for access on localhost
, run wrangler dev --remote
:
$ wrangler devBuilt successfully, built project size is 27 KiB.Using namespace for Workers Site "__app-workers_sites_assets_preview"Uploading site filesListening on http://localhost:8787[2020-05-28 10:42:33] GET example.com/ HTTP/1.1 200 OK[2020-05-28 10:42:35] GET example.com/static/nav-7cb303.png HTTP/1.1 200 OK[2020-05-28 10:42:36] GET example.com/sw.js HTTP/1.1 200 OK
In the output above, you can begin to see log lines for the URLs being requested locally.
DevTools
Wrangler supports using the
Chrome DevTools to view logs/sources, set breakpoints, and profile CPU/memory usage. With wrangler dev
running, press the d key in your terminal to open a DevTools session connected to your Worker.
Customize wrangler dev
You can customize how wrangler dev
works to fit your needs. Refer to the wrangler dev
documentation for available configuration options.
Livestream logs from deployed Workers with wrangler tail
To get more insight into currently deployed Workers, run wrangler tail
. wrangler tail
starts a session to livestream logs from a deployed Worker.