编辑-尝试使用旧版本的节点(8.1.0)在我的计算机上仅安装puppeteer之后,它会引发相同的错误。因此,问题必须是在连接到计算机时,它未以正确的节点版本加载。然后的问题是如何使用正确的node.js版本运行pm2部署后挂钩?
尝试使用pm2
deploy部署我的网络抓取应用程序时,我收到一个非常奇怪的错误。在post-install
部署过程的pm2
挂钩期间,即yarn
将各种软件包安装到我的远程计算机上时,就会出现错误(Ubuntu 18.04)。
错误看起来像这样:
....The rest of the yarn installation...
[4/4] Building fresh packages...
error /home/harrison/gql3.0_processors/source/node_modules/puppeteer: Command failed.
Exit code: 1
Command: node install.js
Arguments:
Directory: /home/harrison/gql3.0_processors/source/node_modules/puppeteer
Output:
/home/harrison/gql3.0_processors/source/node_modules/puppeteer/install.js:175
} catch {
^
SyntaxError: Unexpected token {
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
post-deploy hook failed
Deploy failed
这来自我的install.js
文件夹的puppeteer
目录中的node_modules
文件,这是我在项目中使用的依赖项。
我倾向于认为这是由于在远程安装应用程序时节点版本出现某种错误?谁能提供一些指导?
我的pm2
部署文件如下(有关更多信息,请转到此处:https://pm2.keymetrics.io/docs/usage/application-declaration/)
require("dotenv").config({ path: `./envs/.env.production` });
const path = require("path");
let hosts = process.env.HOSTS.split(",");
let hostsBashArgs = process.env.HOSTS.replace(/,/g, " "); // Pass as args to bash script
module.exports = {
apps: [
{
name: process.env.APP_NAME,
args: ["--color"],
interpreter: process.env.NODE_PATH, // Installation of node on my remote machine, it's ––> `/home/harrison/.nvm/versions/node/v13.7.0/bin/`
cwd: path.resolve(process.env.PROJECT_PATH, "current"), // Where post-deploy runs
script: "dist/index.js", // Webpacked server file
instances: process.env.INSTANCES || 0,
exec_mode: "cluster",
env: {
...process.env,
},
},
],
deploy: {
production: {
user: "harrison",
host: hosts,
key: "~/.ssh/id_rsa2",
ref: "origin/master",
repo: process.env.GIT_REPO,
path: process.env.PROJECT_PATH,
"pre-deploy-local": `./deployEnvs.sh ${process.env.PROJECT_PATH} ${hostsBashArgs}`,
//// THIS IS THE STEP WHICH FAILS
"post-deploy": `yarn install --ignore-engines && \
yarn prod:build && \
yarn prod:serve`,
},
},
};
出于某种原因,pm2
部署后脚本未加载到我的.zshrc
文件中,因此它试图使用的Node.js版本不正确。在安装后的阶段,我可以运行printenv
命令(显示$PATH
变量不包括我的nvm
版本)看到此消息。
因此,解决方案是在.zshrc
脚本中专门提供.bashrc
文件(或使用bash的用户使用post-deploy
文件,如下所示:
... The rest of the ecosystem.config.js file ...
deploy: {
production: {
user: "harrison",
host: hosts,
key: "~/.ssh/id_rsa",
ref: "origin/master",
repo: process.env.GIT_REPO,
path: process.env.PROJECT_PATH,
/// Source the user's .zshrc file first!!
"post-deploy": `source ~/.zshrc && \
yarn install --ignore-engines && \
yarn prod:build && \
yarn prod:serve`
}
}
这是必要的,因为我的.zshrc
文件是在nvm环境中加载的文件(并设置了我对更高版本的节点的加载)。该节点版本是yarn在运行安装脚本时所依赖的版本,这就是过时的版本失败的原因。