Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when use vite-build, how to change the port in the production ? nodejs plateform #195

Closed
sperains opened this issue Dec 3, 2024 · 9 comments · Fixed by #198
Closed

when use vite-build, how to change the port in the production ? nodejs plateform #195

sperains opened this issue Dec 3, 2024 · 9 comments · Fixed by #198

Comments

@sperains
Copy link

sperains commented Dec 3, 2024

when i write code like below, and then run vite build, and run node dist/index.js, trigger error

`
import { Hono } from "hono";

const app = new Hono();

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

export default {
fetch: app.fetch,
port: 3333,
};
`

TypeError: Cannot read properties of undefined (reading 'map')
at It.route (file:///Users/sperains/rainswork/hono_app/dist/index.js:1:12694)
at file:///Users/sperains/rainswork/hono_app/dist/index.js:1:29432
at ModuleJob.run (node:internal/modules/esm/module_job:268:25)
at async onImport.tracePromise.proto (node:internal/modules/esm/loader:543:26)
at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:116:5)

Node.js v22.11.0

if i write code like this, it will start two server port, 3000 and 3333
`
import { serve } from "@hono/node-server";
import { Hono } from "hono";

const app = new Hono();

const isProd = process.env["NODE_ENV"] === "production";

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

if (isProd) {
serve({
fetch: app.fetch,
port: 3333,
});
}

export default app;

`

what's wrong about this? sure, i am a Fish. is there anyone who can tell me how to do it ?

@JsonSong89
Copy link

JsonSong89 commented Dec 5, 2024

you should show your vite.config.ts
if you use a build plugin
import build from "@hono/vite-build/node";
the build plugin will append some code at end of dist/output.js
looks like :
image

use "minify: false" to build the dist code, and you should see the twice serve call ( import {serve} from '@hono/node-server' )

I also have this problem,and I can't find a way to hook the mainApp.port .

@sperains
Copy link
Author

@JsonSong89 我放弃用这个打包了, 改用tsup, 轻松加愉快

@JsonSong89
Copy link

@JsonSong89 我放弃用这个打包了, 改用tsup, 轻松加愉快

确实,hono是个好框架,但vite还是不太适配
尤其这种在插件里面藏代码,然后影响构建产物,还不提供hook方式的,过于邪典了.

@q1uxu
Copy link

q1uxu commented Dec 11, 2024

I want to change the port, but no way to do so..

@JsonSong89
Copy link

I want to change the port, but no way to do so..

哈哈 我寻思这框架也不是国产啊,怎么都是国人...
没地方配置port的,他是在构建环节,又创建了一个mainApp ,然后把你的原来的app当做子路由,关键这个环节一没有处理你原来的app的变量,二没有读取环境变量
除了改代码,无解

@JsonSong89
Copy link

看了下源码,发现还是留了一个hook:
不过插件的配置里面写代码,这个说实话还是有点接受不了

{
        plugins: [
            build({
                entry: "./src/apps/hello.ts",
                output: "hono_hello.mjs",
                minify: false,
                entryContentAfterHooks: [(appName, options) => {
                    console.log("entryContentAfterHooks 触发")
                    return ` 
                     import {serve} from '@hono/node-server'
                     const run_port = parseInt(process.env.PORT) || 3002
                     serve({
        fetch: ${appName}.fetch,
        port: run_port,
    }, info => {
        console.log(\`Server is running on \${JSON.stringify(info)}\`);
    })
                     `
                }]
            }),
            devServer({
                entry: "./src/apps/hello.ts"
            }),
        ]
    }

@JsonSong89
Copy link

tsup 也有问题
没法用 import.meta ,等于一下子脱离hono的构建生态了

@yusukebe
Copy link
Member

The port option is introduced in the new version @hono/[email protected]. It resolves this problem.

This is fixed by #198

@sperains
Copy link
Author

The port option is introduced in the new version @hono/[email protected]. It resolves this problem.

This is fixed by #198

ok. thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants