next-auth: Crash on AWS Amplify build: "Building SSR... can't find mongodb"

Description 🐜

Hi! When building my project on AWS Amplify, the build crashes with complaints for missing mongodb in typeorm. Build logs are below. This doesn’t occur locally.

Not sure why this is happening as I don’t use any database in my Next Auth configuration. I saw issue #2358 but I’m not doing any transpilation in my project.

Please let me know if there is more context I could give!

Is this a bug in your own project?

No

How to reproduce ☕️

This is my [...nextauth].js file. I also want to note that my project uses TypeScript for mostly everything else but I left NextAuth in JavaScript for simplicity.

import NextAuth from 'next-auth'
import Providers from 'next-auth/providers'

const GOOGLE_NEEDED_SCOPES = [
  'https://www.googleapis.com/auth/userinfo.profile',
  'https://www.googleapis.com/auth/userinfo.email ',
  'https://www.googleapis.com/auth/business.manage',
  'https://www.googleapis.com/auth/admin.directory.user',
].join(' ')

export default NextAuth({
  providers: [
    Providers.Google({
      clientId: process.env.CLIENT_ID,
      clientSecret: process.env.CLIENT_SECRET,
      scope: GOOGLE_NEEDED_SCOPES,
      authorizationUrl:
        'https://accounts.google.com/o/oauth2/v2/auth?prompt=consent&access_type=offline&response_type=code',
      accessTokenUrl: 'https://oauth2.googleapis.com/token',
    }),
  ],

  callbacks: {
    async jwt(token, user, account, profile, isNewUser) {
      if (account?.accessToken) {
        token.accessToken = account.accessToken
        token.refreshToken = account.refreshToken
      }

      return token
    },

    async session(session, token) {
      session.accessToken = token.accessToken
      session.refreshToken = token.refreshToken

      return session
    },
  },

  secret: process.env.NEXT_AUTH_SECRET,
})

and here is my next.config.js:

module.exports = {
  reactStrictMode: true,
}

Screenshots / Logs 📽

2021-07-28T10:59:17.174Z [INFO]: Starting SSR Build...
2021-07-28T11:00:22.834Z [ERROR]: Error: Command failed with exit code 1: node_modules/.bin/next build
                                  Failed to compile.
                                  ModuleNotFoundError: Module not found: Error: Can't resolve 'mongodb' in '/codebuild/.../node_modules/typeorm/platform'
                                  > Build error occurred
                                  Error: > Build failed because of webpack errors

Environment 🖥

System: OS: macOS 11.2.1 CPU: (8) x64 Apple M1 Memory: 21.89 MB / 16.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 14.15.4 - ~/.nvm/versions/node/v14.15.4/bin/node Yarn: 1.22.10 - /opt/homebrew/bin/yarn npm: 7.6.3 - ~/.nvm/versions/node/v14.15.4/bin/npm Watchman: 2021.06.07.00 - /opt/homebrew/bin/watchman Browsers: Chrome: 92.0.4515.107 Safari: 14.0.3 npmPackages: next: 11.0.1 => 11.0.1 next-auth: ^3.27.3 => 3.27.3 react: 17.0.2 => 17.0.2

Contributing 🙌🏽

No, I am afraid I cannot help regarding this

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

I got this error, I fix setting webpack5: false on next.config.js.

So then we can close this?

Would like to release this summer, but no promises.

@jackHedaya we’ve released the first beta of v4 which doesn’t implicitly require a database dependency if you’re not using one. Can you give it a shot and let us know if it fixes it? npm install next-auth@beta.

After using the beta I have the following error:

ModuleNotFoundError: Module not found: Error: Package path ./client is not exported from package
/opt/build/repo/node_modules/next-auth (see exports field in /opt/build/repo/node_modules/next-auth/package.json)

Are there some breaking changes?

UPDATE: okay it seems some parts are now exported differtently:

next-auth/client now needs to be imported from next-auth/react

e.g.: import {signIn, useSession} from 'next-auth/react' and don’t forget the provider: import { SessionProvider } from "next-auth/react"'

@jackHedaya we’ve released the first beta of v4 which doesn’t implicitly require a database dependency if you’re not using one. Can you give it a shot and let us know if it fixes it? npm install next-auth@beta.

@giladmaker yes, please open a new issue, as the title says, this one is specific for AWS Amplify.

Also, please provide a full reproduction. We have CodeSandbox templates available in the bug report issue form, or you could create a minimal repository having the same issue. This way we can exclude the possibility that the error is in your configuration and really is a bug in next-auth.