next-auth: FirebaseAdapter and Firebase giving "import" error

Adapter type

@next-auth/firebase-adapter

Environment

System Information.


System:
    OS: macOS 12.6
    CPU: (8) x64 Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz
    Memory: 135.65 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.16.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 8.11.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 105.0.5195.125
    Safari: 16.0
  npmPackages:
    next: 12.3.1 => 12.3.1 
    next-auth: ^4.13.0 => 4.13.0 
    react: 18.2.0 => 18.2.0 


  npmPackages:
    @next-auth/firebase-adapter: ^1.0.2 => 1.0.2 

Reproduction URL

https://github.com/bcthakre/nextjs-nextauth-2.git

Describe the issue

I am trying to use the FirebaseAdapter and Firestore to store user information for my NextJS app. However, getting the following error.


import { initializeApp } from "firebase/app";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.@next-auth/firebase-adapter (/Users/bt/Documents/nextjs/nextjs-nextauth-2/.next/server/pages/api/auth/[...nextauth].js:22:18)
    at __webpack_require__ (/Users/ct/Documents/nextjs/nextjs-nextauth-2/.next/server/webpack-api-runtime.js:33:42)

The issue was raised earlier but has not been fully resolved. Things I tried.

  1. Adding .mjs and .cjs to package-lock and next.config file.
  2. Removing and adding different firebase versions. From 9 to 11.

How to reproduce

Copy the github link as well.

[https://github.com/bcthakre/nextjs-nextauth-2.git]

Expected behavior

Should allow to store user and session information in Firestore. Also, need to provide a way to signIn and signOut

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 7
  • Comments: 38 (5 by maintainers)

Most upvoted comments

You better look at this PR - maybe you can even upvote so that we get a bit more love from the maintainers. https://github.com/nextauthjs/next-auth/pull/5628

The fix is out in @next-auth/firebase-adapter@1.0.3

I highly advise not to use the current firebase adapter - for details please check out this link. https://github.com/nextauthjs/next-auth/issues/5049

Looks like the latest release did not include the compiled version of the package

https://github.com/nextauthjs/next-auth/releases/tag/%40next-auth%2Ffirebase-adapter%40v1.0.2 🤔

https://github.com/nextauthjs/next-auth/actions/runs/3214540412/jobs/5255017831#step:6:101

The build was initiated but the output was still wrong.

reverting to 1.0.1 would be a temporary solution.

Thanks @balazsorban44, downgrading to version 1.0.1 solved for me.

Temporarily for this new version of firebase-adapter

In the project, use the next-transpile-modules lib

In version 13 of next we can use it inside next.config.js

experimental: {
    transpilePackages: [
      '@next-auth/firebase-adapter'
    ]
  }

@alpharaoh , what works for me right now is this: insall @next-auth/firebase-adapter@experinental, and then go to your node_modules, next-auth/firebase-adapter, dist, and edit index.js find: if(value.toDate) add ? after the word value. change to if(value?.toDate) save and run, hope it will work for you.