はじめに
@lazarv/react-serverを使っていて沼ったのでまとめておきます
問題
Convexを使ってAPIを作成したところ以下のエラーが出ました
19:24:13 [react-server] [vite] Named export 'createRequire' not found. The requested module 'module' is a CommonJS module, which may not support all module.exports as named exports.
19:24:13 [react-server] CommonJS modules can always be imported via the default export, for example using:
19:24:13 [react-server]
19:24:13 [react-server] import pkg from 'module';
19:24:13 [react-server] const {createRequire} = pkg;
19:24:13 [react-server]
19:24:13 [react-server] [stack]: SyntaxError: [vite] Named export 'createRequire' not found. The requested module 'module' is a CommonJS module, which may not support all module.exports as named exports.
19:24:13 [react-server] CommonJS modules can always be imported via the default export, for example using:
19:24:13 [react-server]
19:24:13 [react-server] import pkg from 'module';
19:24:13 [react-server] const {createRequire} = pkg;
19:24:13 [react-server]
19:24:13 [react-server] at analyzeImportedModDifference (file:///home/jinwatanabe/workspace/tmp/koreda/node_modules/@lazarv/react-server/node_modules/vite/dist/node/module-runner.js:521:11)
19:24:13 [react-server] at RSCModuleRunner.processImport (file:///home/jinwatanabe/workspace/tmp/koreda/node_modules/@lazarv/react-server/node_modules/vite/dist/node/module-runner.js:1129:56)
19:24:13 [react-server] at RSCModuleRunner.cachedRequest (file:///home/jinwatanabe/workspace/tmp/koreda/node_modules/@lazarv/react-server/node_modules/vite/dist/node/module-runner.js:1165:21)
19:24:13 [react-server] [message]: [vite] Named export 'createRequire' not found. The requested module 'module' is a CommonJS module, which may not support all module.exports as named exports.
19:24:13 [react-server] CommonJS modules can always be imported via the default export, for example using:
19:24:13 [react-server]
19:24:13 [react-server] import pkg from 'module';
19:24:13 [react-server] const {createRequire} = pkg;
19:24:13 [react-server]
src/app/api/articles/GET.popular.server.ts
import { ConvexClient } from "convex/browser";
import { api } from "../../../convex/_generated/api";
const convexUrl = import.meta.env.VITE_CONVEX_URL;
if (!convexUrl) {
throw new Error("VITE_CONVEX_URL is not defined");
}
const client = new ConvexClient(convexUrl);
export default async function GetPopular(request: Request) {
const url = new URL(request.url);
const limit = url.searchParams.get("limit")
? Number(url.searchParams.get("limit"))
: 20;
const articles = await client.query(api.articles.getPopular, {
limit,
});
return new Response(JSON.stringify(articles));
}
解決方法
質問してみたところいまPRをリリースしようとしているとのことでした
バージョンを古いものをいれると今は解決できます。
$ npm i @lazarv/react-server@v0.0.0-experimental-44033c0-20250123-2c156f57
おわりに
ものすごく時間を使いましたが解決できてよかったです。