'pm2 deploy production setup : executing post-setup `yarn && yarn dev` bash: yarn: command not found

I'm trying to use pm2 deploy production update to serve my NextJs app on remote server. I've have already installed yarn locally and also on remote server. It successfully check version to 1.22.18

Error:

--> Deploying to production environment
--> on host remote-server-name
  ○ hook pre-setup
  ○ running setup
  ○ cloning [email protected]**************.git
  ○ full fetch
Cloning into '/home/my_next_app/source'...
  ○ executing post-setup `yarn && yarn dev`
bash: yarn: command not found

  post-setup hook failed

Deploy failed
Deploy failed with exit code: 1

Deploy failed
Deploy failed with exit code: 1

ecosystem.config.file

module.exports = {
  apps: [
    {
      name: "my next app",
      script: "yarn",
      args: "dev",
      interpreter: "/bin/bash",
    },
  ],
  deploy: {
    production: {
      user: "***",
      host: remote_server,
      ref: "origin/main",
      repo: "[email protected]*********",
      path: "/home/my_next_app"
      "pre-deploy": "git fetch --all",
      "post-setup": "yarn && yarn dev",
      "post-deploy": "yarn && yarn dev",
    },
  },
};

package.json

"scripts": {
    "dev": "next dev -p 3001",
... }


Solution 1:[1]

Solved it. Problem was on server with bashrc

On server run:

vi .bashrc

Comment out code block below this comment:

# if not running interactively, don't do anything

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Renato Pavlic