auth0 / node-auth0

Node.js client library for the Auth0 platform.
MIT License
633 stars 309 forks source link

fetch is not defined reported on v18 #990

Closed ronfor closed 7 months ago

ronfor commented 7 months ago

Checklist

Description

When Fetch is NOT supplied

The native fetch does not appear to be picked up when using Node v18. I see the following stacktrace:

ReferenceError: fetch is not defined,
    at new BaseAPI (/var/task/index.js:239330:44),
    at new BaseAuthAPI (/var/task/index.js:246051:5),
    at new Database (/var/task/index.js:246099:16),
    at new AuthenticationClient (/var/task/index.js:246617:21),
    at new TokenProvider (/var/task/index.js:246630:33),
    at new TokenProviderMiddleware (/var/task/index.js:246656:28),
    at new ManagementClient (/var/task/index.js:246708:9),

Managment client defined like so:

      const mgmtClient = new ManagementClient({
        domain: 'https://@@@@.uk.auth0.com',
        clientId: '@@@@',
        clientSecret: '@@@@,
      })

And API call:

      await mgmtClient.users.update({ id: '@@@@' }, { user_metadata: { 'cid': '@@@@' }})

When Fetch IS supplied

If I do specify the fetch override I see the following error:

ReferenceError: FormData is not defined at UsersManager.createFetchParams (/var/task/index.js:239366:44) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async UsersManager.request (/var/task/index.js:239335:27) at async UsersManager.update (/var/task/index.js:243945:22)

Managment client defined like so:

      import fetch from 'node-fetch'

      const mgmtClient = new ManagementClient({
        domain: 'https://@@@@.uk.auth0.com',
        clientId: '@@@@',
        clientSecret: '@@@@,
        fetch
      })

Reproduction

  1. Initalise ManagementClient using client id/secret
  2. Make request to API

Additional context

No response

node-auth0 version

4.3.1

Node.js version

18

ewanharris commented 7 months ago

@ronfor The Node.js docs state that fetch global (and FormData) should be available unflagged from v18.0.0 so the error you're getting is quite surprising.

Are you running your application in an environment that potentially customises the runtime in anyway?

ronfor commented 7 months ago

Yes the --no-experimental-fetch was set without my knowledge. Thanks!

joshmedeski commented 6 months ago

I was on Node 16 which does not have fetch installed globally. Upgrading to Node 20 worked, thanks!