TypeError: GoTrue is not a constructor

I’m trying the following:

const { GoTrue } = require('gotrue-js');

const auth = new GoTrue({
  APIUrl: "https://hm-pantry.netlify.app/.netlify/identity",
  audience: "",
  setCookie: false
});

I’ve tried looking through the issues of the repo but there doesn’t seem to be any solid documentation. Would appreciate any help. My aim is to be able to write some tests for my functions and that requires authentication.

I get the following error when trying this: TypeError: GoTrue is not a constructor

It looks like you are trying to destructure a GoTrue method from the gotrue-js package. Note that there is not GoTrue method. You’ll want to just import the package as GoTrue. Like so:

const GoTrue = require('gotrue-js')

With no curly braces. Let me know if that helps.

I got the same issue when trying it without Dennis. I thought I was doing something wrong.
I should add that this is running as part of a Jest test so not sure if that changes something. I am trying to test another Netlify function that I want to work with authentication but not sure how to get it to create a session in the test so that I can pull it out in the context of the Netlify function

Hey @bikingbadger,
Shooting in the dark here but have you tried import instead of require?

If you’d be willing to share a repo, that might help us as well.

I did actually start with the import like the docs but it didn’t work. So that is why I converted it to require. Wondering whether this has something to do with it being in a function.

I’d really appreciate an example repo if you have one. No better way of learning than seeing a working example

Hmm, a lambda function relies on running in the AWS lambda environment. Not sure how you would mock a lot of those things so you can test locally which does go beyond the scope of Support a bit. But if you share your function and testing code, might give us enough details to help point you in the right direction.

Decided to go with Auth0 and it’s working fine. Thanks for the help and apologies for the trouble

1 Like

No worries, glad you found a working solution!