node:19.4.0
next:14.2.5
// next.config.js
const nextConfig = {
output: "standalone",
...
};
next build
之后会生成一个.next文件夹- 在根目录下创建编写
server.js
文件
const path = require("path");
const address = require("address");
const copyDir = require("copy-dir");
// 拷贝外层public文件夹到standalone文件夹下
copyDir.sync(path.join(__dirname, "public"), path.join(__dirname, ".next/standalone/public"), { cover: true });
// 拷贝static到standalone/.next文件夹下
copyDir.sync(path.join(__dirname, ".next/static"), path.join(__dirname, ".next/standalone/.next/static"), { cover: true });
// 设置IP
const HOSTNAME = address.ip();
process.env.HOSTNAME = HOSTNAME;
// 启动打包后的next项目
require("./.next/standalone/server.js");
- package.json添加启动命令
"scripts": {
"dev": "cross-env next dev",
"build": "cross-env next build",
"start": "next start",
"start:3343": "next start -p 3343",
"server:standalone": "node ./.next/standalone/server.js",
"server:standalone:3343": "cross-env PORT=3343 node ./.next/standalone/server.js",
// 添加的命令
"server": "cross-env PORT=3343 node ./server.js"
},
- 启动
npm run server
> xxxxxxx@0.1.0 server
> cross-env PORT=3343 node ./server.js
▲ Next.js 14.2.5
- Local: http://xxx.xxx.x.xxx:3343
- Network: http://xxx.xxx.x.xxx:3343
✓ Starting...
✓ Ready in 120ms
- Dockerfile
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。