服务器部署

参考网页 https://www.cnblogs.com/alvin-niu/p/9502286.html 1.编译 复制dist 文件夹 2.建立启动配置文件 app.js //定义目录 const fs = require('fs'); const path = require('path'); const express = require('express'); const app = express(); //vue目录 app.use(express.static(path.resolve(__dirname, './dist'))) app.get('*', function(req, res) { const html = fs.readFileSync(path.resolve(__dirname, './dist/index.html'), 'utf-8') res.send(html) }) //定义启动的端口号 app.listen(8082); 3.启动项目 npm install 安装依赖 pm2 start app.js 如果启动不成功需要查看报错 pm2 logs 比如找不到模块 express 在项目目录执行 npm install express 如果未安装pm2 则先进行安装 pm2 在安装过程中可能会出现如下报错 ``` gyp ERR! configure error gyp ERR! stack Error: EACCES: permission denied, mkdir '/www/web/node_modules/node-sass/.node-gyp' gyp ERR! System Linux 3.10.0-1160.45.1.el7.x86_64 gyp ERR! command "/home/node/node-v13.0.0-linux-x64/bin/node" "/www/web/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library=" gyp ERR! cwd /www/web/node_modules/node-sass gyp ERR! node -v v13.0.0 gyp ERR! node-gyp -v v3.8.0 gyp ERR! not ok Build failed with error code: 1 npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^2.3.2 (node_modules/shifty/node_modules/fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"}) npm WARN notsup Unsupported engine for browserslist@4.19.1: wanted: {"node":"^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7"} (current: {"node":"13.0.0","npm":"6.12.0"}) npm WARN notsup Not compatible with your version of node/npm: browserslist@4.19.1 npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.7 (node_modules/watchpack-chokidar2/node_modules/chokidar/node_modules/fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"}) npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.7 (node_modules/webpack-dev-server/node_modules/chokidar/node_modules/fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"}) npm WARN co-defer@1.0.0 requires a peer of co@4 but none is installed. You must install peer dependencies yourself. npm WARN @vue/cli-service@4.5.15 requires a peer of @vue/compiler-sfc@^3.0.0-beta.14 but none is installed. You must install peer dependencies yourself. npm WARN sass-loader@8.0.2 requires a peer of fibers@>= 3.1.0 but none is installed. You must install peer dependencies yourself. npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! node-sass@4.14.1 postinstall: `node scripts/build.js` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the node-sass@4.14.1 postinstall script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! /root/.npm/_logs/2021-12-18T08_04_39_395Z-debug.log ``` 以上报错是因为 没有权限的问题。使用以下指令即可。 ``` npm install --unsafe-perm ```