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!

Hi, I have the same issue with the GoTrue library: TypeError: GoTrue is not a constructor
I tried with require also and it doesn’t work.
Repo is: GitHub - ClaudiaGiv/sveltekit-tailwindcss-board.

If you found already a solution for this, it would be of much help for me.
Thank you!

Hi @ClaudiaGiv,

Could you guide us on how to test your code? I downloaded it and ran npm inpm run dev and I don’t see the error. How do I trigger it?

Hi @hrishikesh

Locally it works but it doesn’t work on netlify: Netlify App

Hi there @ClaudiaGiv can you please post the complete deploy log (either linked to or copied and pasted)

Thanks.

6:09:17 PM: Build ready to start
6:09:19 PM: build-image version: c6001ed68662a13e5deb24abec2b46058c58248a
6:09:19 PM: build-image tag: v3.9.0
6:09:19 PM: buildbot version: ec3023c261ea976fed2230c267a6d92f413f9260
6:09:19 PM: Fetching cached dependencies
6:09:19 PM: Starting to download cache of 159.1MB
6:09:21 PM: Finished downloading cache in 1.902516416s
6:09:21 PM: Starting to extract cache
6:09:26 PM: Finished extracting cache in 4.767463057s
6:09:26 PM: Finished fetching cache in 6.714003282s
6:09:26 PM: Starting to prepare the repo for build
6:09:26 PM: Preparing Git Reference refs/heads/master
6:09:27 PM: Parsing package.json dependencies
6:09:27 PM: Different build dir detected, going to use the one specified in the Netlify configuration file: '' versus '/' in the Netlify UI
6:09:27 PM: Different publish path detected, going to use the one specified in the Netlify configuration file: 'build' versus 'public' in the Netlify UI
6:09:27 PM: Starting build script
6:09:28 PM: Installing dependencies
6:09:28 PM: Python version set to 2.7
6:09:28 PM: Started restoring cached node version
6:09:31 PM: Finished restoring cached node version
6:09:31 PM: v16.3.0 is already installed.
6:09:32 PM: Now using node v16.3.0 (npm v7.15.1)
6:09:32 PM: Started restoring cached build plugins
6:09:32 PM: Finished restoring cached build plugins
6:09:32 PM: Attempting ruby version 2.7.1, read from environment
6:09:33 PM: Started restoring cached ruby version
6:09:33 PM: Finished restoring cached ruby version
6:09:35 PM: Using ruby version 2.7.1
6:09:35 PM: Using PHP version 5.6
6:09:35 PM: Started restoring cached node modules
6:09:35 PM: Finished restoring cached node modules
6:09:35 PM: Started restoring cached go cache
6:09:35 PM: Finished restoring cached go cache
6:09:35 PM: go version go1.14.4 linux/amd64
6:09:35 PM: go version go1.14.4 linux/amd64
6:09:35 PM: Installing missing commands
6:09:35 PM: Verify run directory
6:09:36 PM: ​
6:09:36 PM: ────────────────────────────────────────────────────────────────
6:09:36 PM:   Netlify Build                                                 
6:09:36 PM: ────────────────────────────────────────────────────────────────
6:09:36 PM: ​
6:09:36 PM: ❯ Version
6:09:36 PM:   @netlify/build 18.2.1
6:09:36 PM: ​
6:09:36 PM: ❯ Flags
6:09:36 PM:   baseRelDir: true
6:09:36 PM:   deployId: 6115399d96c5b100070e8a06
6:09:36 PM: ​
6:09:36 PM: ❯ Current directory
6:09:36 PM:   /opt/build/repo
6:09:36 PM: ​
6:09:36 PM: ❯ Config file
6:09:36 PM:   /opt/build/repo/netlify.toml
6:09:36 PM: ​
6:09:36 PM: ❯ Context
6:09:36 PM:   production
6:09:36 PM: ​
6:09:36 PM: ────────────────────────────────────────────────────────────────
6:09:36 PM:   1. build.command from netlify.toml                            
6:09:36 PM: ────────────────────────────────────────────────────────────────
6:09:36 PM: ​
6:09:36 PM: $ npm run build
6:09:36 PM: > ~TODO~@0.0.1 build
6:09:36 PM: > svelte-kit build
6:09:37 PM: vite v2.3.6 building for production...
6:09:37 PM: transforming...
6:09:39 PM: warn - You have enabled the JIT engine which is currently in preview.
6:09:39 PM: warn - Preview features are not covered by semver, may introduce breaking changes, and can change at any time.
6:09:41 PM: (node:1456) [DEP0148] DeprecationWarning: Use of deprecated folder mapping "./" in the "exports" field module resolution of the package at /opt/build/repo/node_modules/tslib/package.json.
6:09:41 PM: Update this package.json to use a subpath pattern like "./*".
6:09:41 PM: (Use `node --trace-deprecation ...` to show where the warning was created)
6:09:42 PM: ✓ 220 modules transformed.
6:09:43 PM: rendering chunks...
6:09:48 PM: .svelte-kit/output/client/_app/manifest.json                               3.29kb
6:09:48 PM: .svelte-kit/output/client/_app/error.svelte-c5a90755.js                    1.19kb / brotli: 0.57kb
6:09:48 PM: .svelte-kit/output/client/_app/pages/index.svelte-b1056c44.js              1.76kb / brotli: 0.79kb
6:09:48 PM: .svelte-kit/output/client/_app/pages/__layout.svelte-e52efd18.js           4.86kb / brotli: 1.50kb
6:09:48 PM: .svelte-kit/output/client/_app/pages/board2.svelte-a2a62ef2.js             3.52kb / brotli: 1.26kb
6:09:48 PM: .svelte-kit/output/client/_app/pages/register.svelte-d89f6332.js           5.24kb / brotli: 1.53kb
6:09:48 PM: .svelte-kit/output/client/_app/start-3789a8dc.js                           18.65kb / brotli: 5.73kb
6:09:48 PM: .svelte-kit/output/client/_app/pages/board.svelte-ac5b5056.js              21.78kb / brotli: 3.82kb
6:09:48 PM: .svelte-kit/output/client/_app/pages/board1.svelte-0e194a99.js             15.48kb / brotli: 4.24kb
6:09:48 PM: .svelte-kit/output/client/_app/chunks/board-45ce14ab.js                    0.61kb / brotli: 0.25kb
6:09:48 PM: .svelte-kit/output/client/_app/chunks/go-true-b1e0ebcb.js                  0.16kb / brotli: 0.14kb
6:09:48 PM: .svelte-kit/output/client/_app/assets/start-0826e215.css                   0.18kb / brotli: 0.11kb
6:09:48 PM: .svelte-kit/output/client/_app/pages/login.svelte-96a7a7a6.js              2.88kb / brotli: 1.05kb
6:09:48 PM: .svelte-kit/output/client/_app/assets/pages/board2.svelte-ce4806e6.css     0.50kb / brotli: 0.18kb
6:09:48 PM: .svelte-kit/output/client/_app/assets/pages/board3.svelte-78437c54.css     0.23kb / brotli: 0.09kb
6:09:48 PM: .svelte-kit/output/client/_app/pages/board3.svelte-4e399f3e.js             20.55kb / brotli: 5.48kb
6:09:48 PM: .svelte-kit/output/client/_app/assets/pages/__layout.svelte-dac65d4e.css   27.61kb / brotli: 2.97kb
6:09:48 PM: .svelte-kit/output/client/_app/chunks/vendor-1963d08a.js                   101.20kb / brotli: 24.38kb
6:09:48 PM: vite v2.3.6 building SSR bundle for production...
6:09:48 PM: transforming...
6:09:49 PM: "flip" is imported from external module "svelte/animate" but never used in "src/routes/board2.svelte" and "src/routes/board3.svelte".
6:09:49 PM: ✓ 48 modules transformed.
6:09:49 PM: rendering chunks...
6:09:49 PM: .svelte-kit/output/server/app.js   110.03kb
6:09:49 PM: Run npm run preview to preview your production build locally.
6:09:49 PM: 
6:09:49 PM: > Using @sveltejs/adapter-netlify
6:09:49 PM:  > .svelte-kit/output/server/app.js:7:7: warning: Ignoring this import because "node_modules/graphql/language/printer.js" was marked as having no side effects
6:09:49 PM:     7 │ import "graphql/language/printer.js";
6:09:49 PM:       ╵        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6:09:49 PM:    node_modules/graphql/package.json:9:2: note: "sideEffects" is false in the enclosing "package.json" file
6:09:49 PM:     9 │   "sideEffects": false,
6:09:49 PM:       ╵   ~~~~~~~~~~~~~
6:09:49 PM: > GoTrue is not a constructor
6:09:49 PM: TypeError: GoTrue is not a constructor
6:09:49 PM:     at file:///opt/build/repo/.svelte-kit/output/server/app.js:965:14
6:09:49 PM:     at ModuleJob.run (node:internal/modules/esm/module_job:175:25)
6:09:49 PM:     at async Loader.import (node:internal/modules/esm/loader:178:24)
6:09:49 PM:     at async prerender (file:///opt/build/repo/node_modules/@sveltejs/kit/dist/chunks/index7.js:80:14)
6:09:49 PM:     at async Object.prerender (file:///opt/build/repo/node_modules/@sveltejs/kit/dist/chunks/index7.js:312:5)
6:09:49 PM:     at async adapt (file:///opt/build/repo/node_modules/@sveltejs/adapter-netlify/index.js:33:4)
6:09:49 PM:     at async adapt (file:///opt/build/repo/node_modules/@sveltejs/kit/dist/chunks/index7.js:338:2)
6:09:49 PM:     at async file:///opt/build/repo/node_modules/@sveltejs/kit/dist/cli.js:654:5
6:09:50 PM: ​
6:09:50 PM: ────────────────────────────────────────────────────────────────
6:09:50 PM:   "build.command" failed                                        
6:09:50 PM: ────────────────────────────────────────────────────────────────
6:09:50 PM: ​
6:09:50 PM:   Error message
6:09:50 PM:   Command failed with exit code 1: npm run build
6:09:50 PM: ​
6:09:50 PM:   Error location
6:09:50 PM:   In build.command from netlify.toml:
6:09:50 PM:   npm run build
6:09:50 PM: ​
6:09:50 PM:   Resolved config
6:09:50 PM:   build:
6:09:50 PM:     base: /opt/build/repo
6:09:50 PM:     command: npm run build
6:09:50 PM:     commandOrigin: config
6:09:50 PM:     environment:
6:09:50 PM:       - NODE_VERSION
6:09:50 PM:       - VITE_FAUNA_API
6:09:50 PM:       - VITE_FAUNA_KEY
6:09:50 PM:     publish: /opt/build/repo/build
6:09:50 PM:     publishOrigin: config
6:09:50 PM:   functionsDirectory: /opt/build/repo/functions
6:09:50 PM: Caching artifacts
6:09:50 PM: Started saving node modules
6:09:50 PM: Finished saving node modules
6:09:50 PM: Started saving build plugins
6:09:50 PM: Finished saving build plugins
6:09:50 PM: Started saving pip cache
6:09:50 PM: Finished saving pip cache
6:09:50 PM: Started saving emacs cask dependencies
6:09:50 PM: Finished saving emacs cask dependencies
6:09:50 PM: Started saving maven dependencies
6:09:50 PM: Finished saving maven dependencies
6:09:50 PM: Started saving boot dependencies
6:09:50 PM: Finished saving boot dependencies
6:09:50 PM: Started saving rust rustup cache
6:09:50 PM: Finished saving rust rustup cache
6:09:50 PM: Started saving go dependencies
6:09:50 PM: Finished saving go dependencies
6:09:50 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
6:09:50 PM: Creating deploy upload records
6:09:50 PM: Failing build: Failed to build site
6:09:50 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2
6:09:50 PM: Finished processing build request in 31.190567658s

This is not an issue specific to Netlify build. If you run npm run build locally, you can still see the error. I don’t know how Svelte Kit works but I think it’s having some issue with requiring the library somehow.

I know this is not an issue with GoTrue as I’m using it in one of my projects currently and using esbuild to bundle the JS and it’s working fine. Maybe you can try using a different JS bundler to see if you’re getting different results.