简体   繁体   中英

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

I'm getting a very strange error when attempting to deploy my web-scraping application using 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).

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.

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/ )

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

Thus, the solution was to specifically source the .zshrc file (or .bashrc file for users using bash) during the post-deploy script, like this:

  ... 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). That node version is what yarn relies upon when running the install script, which is why the outdated version was failing.

To update the created pm2 app with your node version follow the steps:

  1. nvm use (version). Example nvm use 14.17.0.
  2. NODE_ENV=production pm2 restart nameOfYourApp --update-env

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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