Using pm2 to do yarn start gives error while npm start works fine

2024-06-17
978 字约 4 分钟
...

I have a NodeJs microservice. Doing yarn start normally works perfectly fine. When I try to use pm2 to start this as a background service, facing the below issue:

/Users/sairamk/.pm2/logs/api-error-21.log last 15 lines:
21|api     | /usr/local/Cellar/yarn/0.27.5_1/bin/yarn:2
21|api     | PREFIX="/usr/local" exec "/usr/local/Cellar/yarn/0.27.5_1/libexec/bin/yarn.js" "$@"
21|api     |                     ^^^^
21|api     |
21|api     | SyntaxError: Unexpected identifier
21|api     |     at createScript (vm.js:74:10)
21|api     |     at Object.runInThisContext (vm.js:116:10)
21|api     |     at Module._compile (module.js:533:28)
21|api     |     at Object.Module._extensions..js (module.js:580:10)
21|api     |     at Module.load (module.js:503:32)
21|api     |     at tryModuleLoad (module.js:466:12)
21|api     |     at Function.Module._load (module.js:458:3)
21|api     |     at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:70:21)
21|api     |     at Module._compile (module.js:569:30)
21|api     |     at Object.Module._extensions..js (module.js:580:10)

PM2 command that I use:

pm2 start yarn --name api -- start

while npm start for the same, works fine with below command :

pm2 start npm --name api -- start

Tried exploring many possibilities. What am I doing wrong ?

1

The error you're getting is because a bash script (yarn) is being executed with node...

pm2's default interpreter is set to node.


To run yarn (bash script) correctly, you'll have to set pm2's interpreter to bash:

shell:

pm2 start yarn --interpreter bash --name api -- start

or when you are using the ecosystem.config.js:

module.exports = {
  apps : [{
    name      : 'yarn',
    script    : 'yarn',
    args      : 'start',
    interpreter: '/bin/bash',
    env: {
      NODE_ENV: 'development'
    }
  }]
};

 

Your yarn command is pointing to a bash script /usr/local/Cellar/yarn/0.27.5_1/bin/yarn and not the yarn.js in the same folder. By manually running something like

pm2 start /usr/local/Cellar/yarn/0.27.5_1/bin/yarn.js --name api -- start

it should work as expected. Just adapt the path to the current location. In my case it was /usr/share/yarn/bin/yarn.js.


As a sidenote: executing yarn like this takes up almost double the memory than compared to npm. I don't know the exact reason, but I'll stick to npm for pm2 since it should not make any difference, in theory...

 

如果您觉得这篇文章有帮助,请点个赞吧~

分享文章

相关文章

更多文章 →
pm22024-06-18
pm2部署next项目_pm2 启动nextjs
1.全局安装cli 2.简单启动某个 服务 3.启动nextjs服务 4.查看运行是否正常 5.如果 状态不是online 查看log找问题 6.如果是这种报错 SyntaxError: missing ) after argument list 那我们需要这样来启动nextjs项目 具体参考地址 https://github.com/yarnpkg/yarn/issues/6124 7.如果需要开启cluster 集群模式
学习
八股文2026-07-30
一、TypeScript与类型系统(20题)
1\. 在定义AI接口返回的嵌套数据结构(如多轮对话、工具调用结果)时,如何用TypeScript的泛型与条件类型实现灵活的类型推导? 场景:为“AI助手”定义统一的消息类型系统,需兼容不同模型供应商(OpenAI/Anthropic)及内部(Moonshot)的返回格式。 核心答案: 构建一个以泛型为基础、条件类型为分支的层次化类型系统。核心是利用泛型参数化“消息内容”、“工具调用”和“提供商”,再通过条件类型精确匹配不同场景下的数据...
学习
godot2026-07-27
Godot 4 常用 UI 节点详解
Godot 4 常用 UI 节点详解 在 Godot 4 中,UI 系统基于 Control(控件) 节点构建。所有 UI 节点都继承自 Control,形成一棵完整的 UI 树。与游戏引擎中常见的"Canvas + DOM"模式不同,Godot 的 UI 系统是声明式的——你在场景中搭好节点树,引擎自动完成布局计算。 一、布局系统:Container 家族 Container 是 Godot UI 的 骨架 。它决定了子节点的大小和位...
学习
cocos2026-07-22
🎯 Cocos Creator 生命周期详解
Cocos Creator 生命周期详解 一、前言 Cocos Creator 作为主流的游戏开发引擎,其组件化架构的核心就是 生命周期回调函数 。理解这些回调的触发时机、执行顺序和使用场景,是写出高质量游戏逻辑的基础。 本文基于 Cocos Creator 3.x ,系统梳理从引擎启动到场景切换、从节点创建到销毁的完整生命周期。 二、组件生命周期回调总览 Cocos Creator 的生命周期回调按触发顺序排列如下: | 回调函数 |...
学习
cocos2026-07-22
🚀 Cocos Creator 新手入门指南
一、Cocos Creator 是什么 Cocos Creator 是一款专注于 2D 和 3D 游戏开发的引擎,由 Cocos 团队开发。它的核心理念是 组件化开发 ——把游戏中的每个功能拆成独立的组件,像搭积木一样组合起来。 主要特点 : 完全免费,支持发布到 Web、iOS、Android、桌面等多平台 使用 TypeScript 编写脚本,对前端开发者非常友好 编辑器可视化操作,所见即所得 内置资源管理、动画系统、UI 系统、物...
学习
前端2026-07-17
HTML Form 可以把 POST 响应加载进 iframe
在接入支付验证、身份认证或第三方授权页面时,我们经常会遇到这样的流程: 1. 后端返回一个第三方接口地址。 2. 这个地址必须通过 请求访问。 3. 接口返回 HTML,或者重定向到真正的验证页面。 4. 验证页面需要嵌入当前网站,而不是打开新标签页。 5. 验证完成后,当前页面继续执行后续业务。 第一次遇到这个需求,很容易想到: 但涉及第三方支付时,这种方式通常会遇到 CORS、Cookie、重定向和跨域页面读取限制。 实际上,HTM...
学习

评论

请登录后发表评论

去登录
加载评论中...