How do you enable support for async local storage?

If I try to import node:async_hooks I get an error:
Are you trying to bundle for Node? [Obviously] You can use "platform: 'node'
Are you trying to bundle for Node? [Obviously] You can use "platform: 'node'
- 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
zegevlier
zegevlier5mo ago
Cloudflare Docs
Node.js compatibility · Cloudflare Workers docs
Implemented Node.js runtime APIs and enablement instructions for your Worker project.
zegevlier
zegevlier5mo ago
As this is part of the node compatibility, you will need to enable that 😄
Mitya
Mitya5mo ago
I already have I should have said My TOML has:
compatibility_date = "2023-11-21"
node_compat = true
compatibility_date = "2023-11-21"
node_compat = true
zegevlier
zegevlier5mo ago
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.
Mitya
Mitya5mo ago
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)
zegevlier
zegevlier5mo ago
Yup
Mitya
Mitya5mo ago
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?
zegevlier
zegevlier5mo ago
Next to the compatibilty date, you also need compatibility_flags = [ "nodejs_compat" ] in your wrangler.toml
Mitya
Mitya5mo ago
OK 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 imported
zegevlier
zegevlier5mo ago
Ah, 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.
Mitya
Mitya5mo ago
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.
zegevlier
zegevlier5mo ago
No, not really. ALS are supposed to solve that problem 😅
Mitya
Mitya5mo ago
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?
zegevlier
zegevlier5mo ago
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.
Mitya
Mitya5mo ago
Sure. OK, thanks anyway 🙏
kian
kian5mo ago
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)
Mitya
Mitya5mo ago
Ah nice one. So will that just be a case of keeping an eye on future Wrangler versions?
kian
kian5mo ago
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
aroman
aroman2mo ago
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:
▲ [WARNING] The package "node:async_hooks" wasn't found on the file system but is built into node.
▲ [WARNING] The package "node:async_hooks" wasn't found on the file system but is built into node.
kian
kian2mo ago
Wouldn’t surprise me if it’s Wrangler not knowing that nodejs_als should mark that package as external
kian
kian2mo ago
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
aroman
aroman2mo ago
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...
aroman
aroman2mo ago
fortunately the code works fine, so i'm just gonna ignore that