honox icon indicating copy to clipboard operation
honox copied to clipboard

Internal server error: Can't find variable: require

Open bene opened this issue 1 year ago • 3 comments

What version of HonoX are you using?

0.1.23

What steps can reproduce the bug?

I want do create an upload route which uses the @aws-sdk/client-s3 package. However when I use it as I do normally the error below gets thrown. I tried the minio package which also didn't work. I guess it's a common issue with packages?

  1. Start new HonoX project: bun create hono@latest
  2. Install S3 client: bun add @aws-sdk/client-s3
  3. Create
// app/routes/api/upload.ts
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { Hono } from "hono";

const app = new Hono();

const s3 = new S3Client({
  region: process.env.S3_REGION as string,
  forcePathStyle: true,
  endpoint: process.env.S3_URL as string,
  credentials: {
    accessKeyId: process.env.S3_ACCESS_KEY_ID as string,
    secretAccessKey: process.env.S3_ACCESS_KEY_SECRET as string,
  },
});

app.post("/", (c) => {
  // Use s3 client
});

export default app;

What is the expected behavior?

Be able to use S3 client without issues.

What do you see instead?

1:43:51 PM [vite] Error when evaluating SSR module /Users/bene/Code/share/app/server.ts:
|- ReferenceError: Can't find variable: require
    at <anonymous> (:5:26)
    at anonymous (:1:26)
    at <anonymous> (/Users/bene/Code/share/node_modules/vite/dist/node/chunks/dep-C1-ZB6nQ.js:53450:5)
    at processTicksAndRejections (:12:39)

1:43:51 PM [vite] Internal server error: Can't find variable: require
      at <anonymous> (:5:26)
      at anonymous (:1:26)
      at <anonymous> (/Users/bene/Code/share/node_modules/vite/dist/node/chunks/dep-C1-ZB6nQ.js:53450:5)
      at processTicksAndRejections (:12:39)

Additional information

No response

bene avatar Jul 02 '24 11:07 bene

Hi @bene

I think It's a Vite issue. I've investigated for a while, but I could not find a good solution. It may not support that library.

yusukebe avatar Jul 05 '24 04:07 yusukebe

I ran into the same problem myself, but I put import require from "node:require" on the code which throw error (but not good idea)

or https://github.com/vitejs/vite/issues/3409

EdamAme-x avatar Jul 05 '24 04:07 EdamAme-x

The same issue happened when I tried to use the mysql2 driver.

cometkim avatar Nov 15 '24 05:11 cometkim