question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

🚀 Feature Request: Detect whether code is being run in Cloudflare Workers (or Node.js)

See original GitHub issue

Describe the solution

Add some kind of macro to detect whether the code, that is currently being run, is running in Cloudflare Workers.

For example with esbuild:

return esbuild.build({
  define: {
    IS_CLOUDFLARE_WORKER: 'true'
  }
})

I’ve been using this for my custom Cloudflare Worker bundling (see github.com/brillout/build-worker/tree/build-worker.mjs#L20-L22).

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
WalshyDevcommented, Jul 16, 2022

I believe the Navigator API is the preferred way to do this

if (navigator.userAgent === 'Cloudflare-Workers') {
  // logic
}
0reactions
petebacondarwincommented, Jul 18, 2022

I just tried this in a worker:

export default {
  async fetch() {) {
    return new Response(JSON.stringify(navigator));
  }
}

and in the browser, I see:

{"userAgent":"Cloudflare-Workers"}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Possible to get PoP running worker code?
Hi, I just started using workers a couple of days ago to do some load balancing and failover depending on region of the...
Read more >
Develop, test, and deploy Cloudflare Workers with Denoflare
Denoflare is a small framework that lets you publish Cloudflare Workers written in Deno. Here's how to develop, test, and deploy with it....
Read more >
[Feature Request] Using stripe-node outside of Node.js (e.g. ...
I'm trying to setup a Stripe service as a serverless function using the Cloudflare Workers platform. But the stripe-node package seems to ...
Read more >
Running RedwoodJS on Cloudflare Workers
In general, I foresee it being frustrating if my local dev environment spins up the API in a Node runtime so it's not...
Read more >
What Cloudflare is up to featuring Jon Kuperman (JS Party ...
Thanks to listener Matt Mannucci for requesting this episode! ... Node.js application that they have, stick it on Cloudflare Workers, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found