简体   繁体   English

如何告诉 pm2 在部署时使用特定的节点版本

[英]How To Tell pm2 to Use Specific Node Version On Deploy

EDIT -- After attempting to just install puppeteer on my machine using an old version of node (8.1.0), it throws the same error.编辑——尝试使用旧版本的节点(8.1.0)在我的机器上安装 puppeteer 后,它会引发相同的错误。 The problem therefore must be that when connecting to the machine, it's not loading in the correct node version.因此,问题一定是在连接到机器时,它没有加载正确的节点版本。 The question then, is how does one run the pm2 post-deploy hook using the correct node.js version?那么问题是如何使用正确的 node.js 版本运行 pm2 部署后挂钩?

I'm getting a very strange error when attempting to deploy my web-scraping application using pm2 deploy.尝试使用pm2 deploy 部署我的网络抓取应用程序时,我遇到了一个非常奇怪的错误。 The error is coming during the post-install hook of the pm2 deployment process, which is when yarn is installing the various packages to my remote machine (Ubuntu 18.04).错误出现在pm2部署过程post-install挂钩期间,即当yarn将各种软件包安装到我的远程机器(Ubuntu 18.04)时。

The error looks like this:错误如下所示:

....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

This is coming from the install.js file within the puppeteer directory of my node_modules folder, which is a dependency that I'm using for my project.这来自我的node_modules文件夹的puppeteer目录中的install.js文件,这是我用于我的项目的依赖项。

I'm inclined to think that this is due to some sort of error with my node version when I'm installing the application remotely?我倾向于认为这是由于我在远程安装应用程序时节点版本出现某种错误? Can anyone offer some guidance?任何人都可以提供一些指导吗?

My pm2 deploy file is as follows (for more info, go here: https://pm2.keymetrics.io/docs/usage/application-declaration/ )我的pm2部署文件如下(更多信息,go: 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`,
    },
  },
};

For whatever reason, the pm2 post-deploy script was not loading in my .zshrc file, thus the Node.js version that it was trying to use was incorrect.无论出于何种原因, pm2部署后脚本没有加载到我的 .zshrc 文件中,因此它尝试使用的.zshrc版本不正确。 I was able to see this by during the post-install stage, running the printenv command (which showed the $PATH variable did not include my nvm version).我能够在安装后阶段看到这一点,运行printenv命令(显示$PATH变量不包括我的nvm版本)。

Thus, the solution was to specifically source the .zshrc file (or .bashrc file for users using bash) during the post-deploy script, like this:因此,解决方案是在post-deploy脚本期间专门获取.zshrc文件(或使用 bash 的用户的.bashrc文件),如下所示:

  ... 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`
    }
}

This was necessary because my .zshrc file was what loaded in the nvm environment (and sets up my loading of a later version of node).这是必要的,因为我的 .zshrc 文件是在.zshrc环境中加载的(并设置了我对更高版本节点的加载)。 That node version is what yarn relies upon when running the install script, which is why the outdated version was failing.该节点版本是 yarn 在运行安装脚本时所依赖的,这就是过时版本失败的原因。

To update the created pm2 app with your node version follow the steps:要使用您的节点版本更新创建的 pm2 应用程序,请执行以下步骤:

  1. nvm use (version). nvm 使用(版本)。 Example nvm use 14.17.0.示例 nvm 使用 14.17.0。
  2. NODE_ENV=production pm2 restart nameOfYourApp --update-env NODE_ENV=production pm2 restart nameOfYourApp --update-env

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM