honojs / vite-plugins

Vite Plugins for Hono
https://hono.dev
134 stars 35 forks source link

Error running dev server #58

Closed martinkeefe closed 10 months ago

martinkeefe commented 10 months ago

I've setup my-app using the Hono CLI and then changed it following the instructions here. When I run npm exec vite I get this error:

failed to load config from D:\my\temp\hono\my-app\vite.config.ts
error when starting dev server:
Error [ERR_REQUIRE_ESM]: require() of ES Module D:\my\temp\hono\my-app\node_modules\@hono\vite-dev-server\dist\dev-server.js from D:\my\temp\hono\my-app\node_modules\@hono\vite-dev-server\dist\index.cjs not supported.

My vite.config.ts is:

import { defineConfig } from "vite";
import devServer from "@hono/vite-dev-server";

export default defineConfig({
  plugins: [
    devServer({
      entry: "src/index.ts", // The file path of your application.
    }),
  ],
});

package.json is:

{
  "scripts": {
    "dev": "vite"
  },
  "dependencies": {
    "@hono/node-server": "^1.4.1",
    "hono": "^3.12.6"
  },
  "devDependencies": {
    "@hono/vite-dev-server": "^0.4.0",
    "tsx": "^3.12.2",
    "vite": "^5.0.12"
  }
}

and src/index.ts is:

import { Hono } from "hono";

const app = new Hono();

app.get("/", (c) => {
  return c.text("Hello Hono!");
});

export default app;

I'm using Windows 10, node 20.1.0, and npm 9.6.4.

Any ideas on how I can fix the error? TIA

martinkeefe commented 10 months ago

OK, I just needed to add

"type": "module"

to package.json. Seems to be working now. Doh!

yusukebe commented 10 months ago

Yes. You are right.