简体   繁体   English

NEXT AUTH + Firebase 无法在模块外导入语句

[英]NEXT AUTH + Firebase Cannot import statement outside a module

So we have a firebase config file:所以我们有一个 firebase 配置文件:

import { getFirestore } from "firebase/firestore";

export const firebaseConfig = {
  apiKey: process.env.FIREBASE_API_KEY,
  authDomain: process.env.FIREBASE_AUTH_DOMAIN,
  projectId: process.env.FIREBASE_PROJECT_ID,
  storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
  messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,
  appId: process.env.FIREBASE_APP_ID,
  measurementId: process.env.FIREBASE_MEASUREMENT_ID,
};

// Initialize Firebase
const firebaseApp = initializeApp(firebaseConfig);

export const firebaseStore = getFirestore(firebaseApp);

and NEXT AUTH [...nextauth].ts和 NEXT AUTH [...nextauth].ts

import GoogleProvider from "next-auth/providers/google";
import { FirestoreAdapter } from "@next-auth/firebase-adapter";
import { firebaseConfig } from "../../../config/firebase";

export const authOptions = {
  providers: [
    GoogleProvider({
      clientId: process.env.FIREBASE_AUTH_GOOGLE_CLIENT_ID!,
      clientSecret: process.env.FIREBASE_AUTH_WEB_CLIENT_SECRET!,
    }),
  ],
  adapter: FirestoreAdapter(firebaseConfig),
};

export default NextAuth(authOptions);

The problem is that i'm always facing an error问题是我总是遇到错误

Error - H:\JS\ktest\node_modules\@next-auth\firebase-adapter\dist\index.js:1
import { initializeApp } from "firebase/app";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1088:15)
    at Module._compile (node:internal/modules/cjs/loader:1123:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at @next-auth/firebase-adapter (H:\JS\ktest\.next\server\pages\api\auth\[...nextauth].js:22:18)
    at __webpack_require__ (H:\JS\ktest\.next\server\webpack-api-runtime.js:33:42)

I tried changing package.json and tsconfig with "type":"module" and etc but i causes more errors and that shouldn't be a fix for this issue.我尝试用“类型”:“模块”等更改 package.json 和 tsconfig,但我导致了更多错误,这不应该是解决此问题的方法。

I'm struggling for some time with this issue and can't find any answers online anymore, i feel like stackoverflow is my last hope.我在这个问题上苦苦挣扎了一段时间,再也无法在网上找到任何答案,我觉得 stackoverflow 是我最后的希望。

I guess 'firebase-adapter' inside code has problem with module and CommonJS.我猜想代码中的“firebase-adapter”与模块和 CommonJS 有问题。 so, I modifying file directly for solved this problem.所以,我直接修改文件来解决这个问题。 (import -> require) (导入 -> 要求)

in your case 'H:\JS\ktest\node_modules@next-auth\firebase-adapter\dist\index.js' and 'H:\JS\ktest\node_modules@next-auth\firebase-adapter\dist\converter.js'在你的情况下 'H:\JS\ktest\node_modules@next-auth\firebase-adapter\dist\index.js' 和 'H:\JS\ktest\node_modules@next-auth\firebase-adapter\dist\converter.杰斯'

index.js -> index.js ->

const { initializeApp } = require("firebase/app");
const { addDoc, collection, deleteDoc, doc, getDoc, getDocs, getFirestore, limit, query, runTransaction, setDoc, where, connectFirestoreEmulator, } = require("firebase/firestore");
const { getConverter } = require("./converter");
module.exports.FirestoreAdapter = ({ emulator, ...firebaseOptions }) => {

coverter.js -> coverter.js ->

const { Timestamp } = require("firebase/firestore");
const isTimestamp = (value) => typeof value === "object" && value !== null && value instanceof Timestamp;
module.exports.getConverter = (options) => ({

like this way.像这样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不能在模块外使用 import 语句,TypeScript NodeJS Firebase Functions 项目 - Cannot use import statement outside a module, TypeScript NodeJS Firebase Functions project SyntaxError:无法在具有共享 package 的模块外部使用导入语句 - SyntaxError: Cannot use import statement outside a module with shared package service worker 注册失败-Uncaught SyntaxError: Cannot use import statement outside a module (at firebase-messaging-sw.js:1:1) - service worker registration fails-Uncaught SyntaxError: Cannot use import statement outside a module (at firebase-messaging-sw.js:1:1) GeneratedPluginConstraint,致命错误:找不到模块“firebase_auth”@import firebase_auth; - GeneratedPluginConstraint , Fatal error: module 'firebase_auth' not found @import firebase_auth; Next.js TypeError: (0, firebase_auth__WEBPACK_IMPORTED_MODULE_1__.onAuthStateChanged) 不是 function - Next.js TypeError: (0 , firebase_auth__WEBPACK_IMPORTED_MODULE_1__.onAuthStateChanged) is not a function 未找到模块:错误:您尝试导入项目 src/ 目录之外的 /firebase/app - Module not found: Error: You attempted to import /firebase/app which falls outside of the project src/ directory 使用 firebase 适配器的 Next Auth 身份验证不适用于 Next JS 13 - Next Auth Authentication with firebase Adapter not working on Next JS 13 Firebase/auth 导入源地图警告 - Firebase/auth import source-map warnings 属性“auth”在类型“typeof import ...”上不存在 firebase/auth - Property 'auth' does not exist on type 'typeof import..." firebase/auth Next13 不适用于现有的 firebase 身份验证上下文 - Next13 not working with existing firebase auth context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM