How do you enable support for async local storage?
If I try to import
node:async_hooks
I get an error:
- My TOML has node_compat = true
- The ALS docs don't say anything about prerequisites to support ALS
- platform
is not a documented config property that I can see
- This page says I can enable ALS by adding compatibility_flags = [ "nodejs_als" ]
in the TOML but that raises the error _"No such compatibility flag: nodejsals"23 Replies
Cloudflare Docs
Node.js compatibility · Cloudflare Workers docs
Implemented Node.js runtime APIs and enablement instructions for your Worker project.
As this is part of the node compatibility, you will need to enable that 😄
I already have
I should have said
My TOML has:
That's a different type of node compatibility. It's confusing, but these two are seperate and not compatible with eachother.
Remove that one, and add the compatibility date
Note too that
node_als
is only available in very recent versions of Wrangler (if any, I haven't tested it myself). But, if you wish you can enable that instead of the full compatibility flag. Neither of those are compatible with node_compat = true
.You're right, that is confusing 😄 So to be clear, are you saying just remove the
node_compat
entry and keep the date one? I have both currently.
(As per my last message)Yup
So my TOML now has only
compatibility_date = "2023-11-21"
, yet the error remains when I try to import node:async_hooks
_The package "node:async_hooks" wasn't found on the file system but is built into node.
Add "nodecompat = true" to your wrangler.toml file and make sure to prefix the module name with "node:" to enable Node.js compatibility.
The error seems to be telling me to put node_compat
back in?Next to the compatibilty date, you also need
compatibility_flags = [ "nodejs_compat" ]
in your wrangler.tomlOK that now gives a different error, from modules that use
fs
. So it seems with my previous setup they could use fs
, but with this one, I can use ALS but not fs
. Is there any way to use both?
Actually all sorts of bits aren't imported now - stream
, buffer
etc, which previously were being importedAh, I thought you just enabled
node_compat = true
to get ALS. There is currently no way to use both compatibility options, and so if you need fs
, stream
, buffer
, etc. you will not be able to use ALS.Dang. Oh well, thanks anyway, at least I know. Appreciate the help. Can you think of any other way than ALS to store request-scoped globals i.e. which aren't susceptible of leakage/mutation from other requests? That was my impetus here.
No, not really. ALS are supposed to solve that problem 😅
Double dang. I suppose I'll just have to manually pass stuff around between middleware/controllers/helpers etc.
Do you know if there's any planned solution to the above i.e. using both?
AFAIK the idea is to eventually deprecate
node_compat = true
, with the currently missing features being added to the compatibility flag, but that might have changed since then.Sure. OK, thanks anyway 🙏
nodejs_als
will probably work for you eventually
it's a new thing that just hasn't been deployed yet (or hasn't hit everywhere)Ah nice one. So will that just be a case of keeping an eye on future Wrangler versions?
It'll be a runtime update, so it might appear on https://developers.cloudflare.com/workers/platform/changelog/
It won't require anything from Wrangler
sorry to necropost, but can you confirm whether
compatibility_flags = ["nodejs_als"]
is expected to be working on the latest stable version of wrangler (3.60.2)
?
I have that in my wrangler.toml
, but i'm still getting:
Wouldn’t surprise me if it’s Wrangler not knowing that nodejs_als should mark that package as external
Yeah, Wrangler has no idea about that flag so it doesn’t know what to do with it. I’d open an issue on https://github.com/cloudflare/workers-sdk/issues
GitHub
Issues · cloudflare/workers-sdk
⛅️ Home to Wrangler, the CLI for Cloudflare Workers® - Issues · cloudflare/workers-sdk
thanks for confirming! filed here: https://github.com/cloudflare/workers-sdk/issues/6011
GitHub
🐛 BUG: wrangler does not mark
node:async_hooks
as external when `...Which Cloudflare product(s) does this pertain to? Workers Runtime, Wrangler core What version(s) of the tool(s) are you using? 3.60.2 What version of Node are you using? 18 What operating system an...
fortunately the code works fine, so i'm just gonna ignore that