使用pm2 部署react+nextjs项目到服务器
记录一下
next.config.js中:
output: 'standalone',
package.json配置:
"scripts": {"dev": "cross-env NODE_OPTIONS='--inspect' next dev","build": "next build","start": "cp -r .next/static .next/standalone/.next/static && cp -r public .next/standalone/public && cross-env PORT=$npm_config_port HOSTNAME=$npm_config_host node .next/standalone/server.js","lint": "pnpm eslint --cache --cache-location node_modules/.cache/eslint/.eslint-cache","lint-only-show-error": "pnpm eslint --cache --cache-location node_modules/.cache/eslint/.eslint-cache --quiet","fix": "next lint --fix","eslint-fix": "eslint --cache --cache-location node_modules/.cache/eslint/.eslint-cache --fix","eslint-fix-only-show-error": "eslint --cache --cache-location node_modules/.cache/eslint/.eslint-cache --fix --quiet","eslint-complexity": "eslint --rule 'complexity: [error, {max: 15}]' --quiet","prepare": "cd ../ && node -e \"if (process.env.NODE_ENV !== 'production'){process.exit(1)} \" || husky ./web/.husky","gen-icons": "node ./app/components/base/icons/script.mjs","uglify-embed": "node ./bin/uglify-embed","check-i18n": "node ./i18n/check-i18n.js","auto-gen-i18n": "node ./i18n/auto-gen-i18n.js","test": "jest","test:watch": "jest --watch","storybook": "storybook dev -p 6006","build-storybook": "storybook build"},
react+nextjs项目打包后会生成一个.next文件
里面文件目录如下,有很大的cache,可以删掉
看网上的说法是,部署只需要打包出的这个.next文件,但我只用它,部出来的根本无法访问
这个后面再看看吧
安装PM2
npm install pm2@latest -g
在代码文件目录下执行,不是.next/standalone/下:
pm2 start npm --name 自己命名 -- run start
成功