NodeJS MastoJS + NextJS API路由(无服务器):[类型错误]:类扩展值#< Object>不是构造函数或null

umuewwlo  于 2023-05-28  发布在  Node.js
关注(0)|答案(1)|浏览(187)

刚刚尝试使用mastojs与nextjs API路由。如果我通过nodejs从本地机器运行mastojs代码,它就可以工作:

import { login } from 'masto';

const masto = await login({
    url: process.env.URL,
    accessToken: process.env.TOKEN,
  });

const status = await masto.v1.statuses.create({
  status: 'Hello from #mastojs!',
  visibility: 'public',
});

然而,在nextjs API路由中,它给了我一个错误。NextJS中的代码看起来像这样:

import { NextResponse } from "next/server";
import { login } from 'masto';

export async function GET() {
  const masto = await login({
      url: process.env.URL,
      accessToken: process.env.TOKEN,
    });
      
  return NextResponse.json({ 
    hello: "world"
  });  
}

这已经抛出了一个错误:

  • 错误node_modules/.pnpm/masto@5.11.3/node_modules/masto/dist/index.cjs(3822:33)@ EventEmitter
  • error错误[TypeError]:Class extends value # is not a constructor or null at eval(webpack-internal:///(sc_server)/./node_modules/.pnpm/masto@5.11.3/node_modules/masto/dist/index.cjs:3986:38)at Object.(sc_server)/./node_modules/.pnpm/masto@5.11.3/node_modules/masto/dist/index.cjs(/Users/xxx/.next/server/app/API/bots/mastodon/route.js:1982:1)

知道这是什么吗mastojs在无服务器函数中运行有问题吗?

kiz8lqtg

kiz8lqtg1#

可能值得向masto维护人员提交一个bug,因为该库在Next.js中无法开箱即用。
我通过以下步骤解决了所有编译问题:
1.安装可选依赖项:

npm i bufferutil encoding utf-8-validate

1.对patch-package应用以下修补程序:
patches/masto+5.11.3.patch

diff --git a/node_modules/masto/dist/index.cjs b/node_modules/masto/dist/index.cjs
index eb0fb04..7dcae64 100644
--- a/node_modules/masto/dist/index.cjs
+++ b/node_modules/masto/dist/index.cjs
@@ -4,7 +4,7 @@ var ponyfills = require('@mastojs/ponyfills');
 var semver = require('semver');
 var qs = require('qs');
 var changeCase = require('change-case');
-var EventEmitter = require('eventemitter3');
+var EventEmitter = require('eventemitter3').EventEmitter;
 var WebSocket = require('isomorphic-ws');
 
 /**

希望这能帮上忙。

相关问题