Firebase-admin 依赖项 farmhash-modern 会引发 WebAssembly 错误

问题描述 投票:0回答:1

我正在尝试在我的 nextjs + TS 项目上配置 firebase-admin SDK。每当我尝试调用任何 SDK 函数时,都会收到 webAssembly 错误。就我而言,我正在尝试为服务器端 api 配置中间件,当我调用 verifyIdToken 方法时,它会抛出错误。有人遇到过类似的错误吗?我该如何解决这个问题?

以下是我的

middleware.ts
文件:

import { auth } from "./lib/config/firebaseConfig";
import { NextResponse } from "next/server";
import { NextRequest } from "next/server";

interface ExtendedNextRequest extends NextRequest {
  uid?: string;
}

export async function middleware(req: ExtendedNextRequest, res: NextResponse) {
  try {
    let token = req.headers.get("Authorization");
    if (!token)
      return NextResponse.json({ message: "Access Denied" }, { status: 403 });
    if (token.startsWith("Bearer ")) {
      token = token.split(" ")[1];
    }

    let decodedToken = await auth.verifyIdToken(token);
    let uid = decodedToken.uid;
    req.uid = uid;

    return NextResponse.next();
  } catch (error: any) {
    console.log(error.errorInfo);
    return NextResponse.json({ message: "Invalid Token" }, { status: 401 });
  }
}

以下是我收到的错误:

⨯ ./node_modules/farmhash-modern/bin/bundler/farmhash_modern_bg.wasm
Module parse failed: Unexpected character '' (1:0)
The module seem to be a WebAssembly module, but module is not flagged as WebAssembly module for webpack.
BREAKING CHANGE: Since webpack 5 WebAssembly is not enabled by default and flagged as experimental feature.
You need to enable one of the WebAssembly experiments via 'experiments.asyncWebAssembly: true' (based on async modules) or 'experiments.syncWebAssembly: true' (like webpack 4, deprecated).
For files that transpile to WebAssembly, make sure to set the module type in the 'module.rules' section of the config (e. g. 'type: "webassembly/async"').
(Source code omitted for this binary file)

Import trace for requested module:
./node_modules/farmhash-modern/bin/bundler/farmhash_modern_bg.wasm
./node_modules/farmhash-modern/bin/bundler/farmhash_modern.js
./node_modules/farmhash-modern/lib/browser.js
./node_modules/firebase-admin/lib/remote-config/condition-evaluator-internal.js
./node_modules/firebase-admin/lib/remote-config/remote-config.js
./node_modules/firebase-admin/lib/app/firebase-namespace.js
./node_modules/firebase-admin/lib/default-namespace.js
./node_modules/firebase-admin/lib/index.js
./lib/config/firebaseConfig.ts
typescript next.js webassembly firebase-admin
1个回答
0
投票

您遇到的错误与 Webpack 5 中默认未启用 WebAssembly 有关。要解决此问题,您需要在 Next.js 配置中启用 WebAssembly。

以下是解决此问题的步骤:

  1. 更新 Next.js 配置以启用 WebAssembly:

    创建或更新您的

    next.config.js
    文件以包含 WebAssembly 配置。该文件应该位于您项目的根目录中。

    // next.config.js
    module.exports = {
      webpack: (config, { isServer }) => {
        // Enable WebAssembly support
        config.experiments = { asyncWebAssembly: true };
    
        // Add rule to handle WebAssembly files
        config.module.rules.push({
          test: /\.wasm$/,
          type: 'webassembly/async',
        });
    
        return config;
      },
    };
    
  2. 安装必要的依赖项:

    如果尚未安装,请确保已安装

    webpack
    webpack-cli
    。尽管 Next.js 默认附带 Webpack,但将它们作为显式依赖项有时可以帮助解决问题。

    npm install webpack webpack-cli
    
  3. 重新启动您的开发服务器:

    更新配置后,重新启动 Next.js 开发服务器以应用更改。

    npm run dev
    

通过启用 WebAssembly 支持并正确配置 Webpack,应该可以解决

farmhash-modern
依赖项的问题,从而使 Firebase Admin SDK 能够正常运行。如果错误仍然存在,请确保所有依赖项都是最新的,并且项目中没有冲突的配置。

© www.soinside.com 2019 - 2024. All rights reserved.