如果你想在服务器上node服务直接部署Nuxt3项目而不选择PM2或者生产静态文件 可以通过下面方式修改运行端口而不是本地端口
- 打开你的项目,正常运行yarn build/ npm run build
- 构建完成后生成.output文件
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2023/10/22 13:51 public
d----- 2023/10/22 13:52 server
-a---- 2023/10/22 13:55 15010923 .output.zip
-a---- 2023/10/22 13:52 129 nitro.json
- 打开Server文件
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2023/10/22 13:52 .vscode
d----- 2023/10/22 13:52 chunks
d----- 2023/10/22 13:52 node_modules
-a---- 2023/10/22 13:52 389 index.mjs
-a---- 2023/10/22 13:52 104 index.mjs.map
-a---- 2023/10/22 13:52 1457 package.json
- 找到chunks文件夹继续打开
- 打开nitor文件夹
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2023/10/22 13:55 389002 node-server.mjs
-a---- 2023/10/22 13:52 3211 node-server.mjs.map
- node-server.mjs就是我们的目标文件了
- 用vscode或者你喜欢的编译器打开它
- 拉到最底部,找到port变量,把它修改成你实际使用的端口即可
const cert = process.env.NITRO_SSL_CERT;
const key = process.env.NITRO_SSL_KEY;
const server = cert && key ? new Server({ key, cert }, toNodeListener(nitroApp.h3App)) : new Server$1(toNodeListener(nitroApp.h3App));
// const port = destr(process.env.NITRO_PORT || process.env.PORT) || 3e3;
const port = 666 // Change Here!
const host = process.env.NITRO_HOST || process.env.HOST;
const path = process.env.NITRO_UNIX_SOCKET;
const listener = server.listen(path ? { path } : { port, host }, (err) => {
至于为什么不修改.env环境变量,我试了很多次 都不能成功,不知道它这里的环境变量是索引哪的?