Questions involving Node.js compat issues

So far I’ve not had any luck at all trying to use a npm package with workers. For example, I’m trying to use rss-parser. When added, I’m told to add

node_compat = true

to wrangler.toml. However, the docs here (Node.js compatibility · Cloudflare Workers docs), explicitly say to do this instead:

compatibility_flags = [ "nodejs_compat" ]

And if you do both, you get an error:

✘ [ERROR] The `nodejs_compat` compatibility flag cannot be used in conjunction with the legacy `--node-compat` flag. If you want to use the Workers runtime Node.js compatibility features, please remove the `--node-compat` argument from your CLI command or `node_compat = true` from your config file.

So I removed the second one but now I get:

service core:user:rsstojson: Uncaught TypeError: globalThis.XMLHttpRequest is not a constructor

I saw something similar with an earlier test I did. I got the impression that most npm modules should “mostly” just work, but I’m not seeing that. What am I doing wrong?

The newer nodejs_compat flag requires prefixed imports ('node:foo' rather than just 'foo'), not only in your code but in any modules you import. If the modules are still using unprefixed imports for built-in Node modules, they won’t work until they are update to use the prefix.

2 Likes

Hmm, it seems like that’s possibly going to make a lot of npm modules unusable, right?

Prefixed imports have been the “right” way to do it in Node for a while, and Node seems to be moving toward prefix-only, but yeah, a module that hasn’t been updated for it won’t work with nodejs_compat.

The older node_compat, which is polyfills, may work for you if you run into this. I don’t think it’s being updated any more, but if it works, it works.

2 Likes

In this case it’s an RSS parser, and as RSS hasn’t changed in a while, not surprised the code won’t work. I may end up parsing XML myself. Ick. :wink:

Or, modify the module to use a prefixed import and send a PR to the maintainer. That’s easier than parsing XML. :nerd_face:

1 Like

Heh, I found a module that worked well for XML parsing, and then “massaged” it a bit.

This isn’t for production, just a silly blog post.

This makes cloudflare workers pretty useless. Why did they decide that’s a good idea? :smile: