How do I use native fetch when running n8n with node.js 18?

Describe the issue/error/question

I’d like to use the native version of fetch as one would with node.js version 18. Is this supported?
I’m running n8n with node.js 18 set via NODE_VERSION.
Also tried setting NODE_FUNCTION_ALLOW_BUILTIN=https,fetch

What is the error message (if any)?

“ReferenceError: fetch is not defined” when using the Code node.

Information on your n8n setup

  • n8n version: latest as of 3/13/23
  • Database you’re using (default: SQLite): sqlite
  • Running n8n with the execution process [own(default), main]: own
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: docker

Welcome to the community @eddywashere!

Sorry, not totally up-to-date on that topic. Is it only part of Node.js v18? Because the default n8n Docker image currently uses Node.js v16.

as Jan pointed out, our docker image ships with node 16 right now, so there is no built in native fetch api available.

However we do ship with node-fetch. So you can set NODE_FUNCTION_ALLOW_EXTERNAL=node-fetch and then try using fetch by importing the shim via const fetch = require('node-fetch');

It was behind a feature flag in v17. Might be worth tracking someone in case node-fetch causes a conflict when y’all do upgrade. But maybe it already handles when fetch is available globally.

Anyway, this is helpful. I miss understood where NODE_VERSION gets applied and can’t simply be overridden without rebuilding the image

Thanks for the quick responses!