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 集群模式
学习
http-网络2026-10-15
OAuth 2.0是什么,以及如何正确接入
一、先从一个需求理解 OAuth 2.0 假设你开发了一个相册整理工具,用户希望它读取某个云盘里的照片。 把云盘密码交给工具,会带来几个问题:工具拿到的权限太大,密码可能泄漏,用户也难以只撤销这个工具的访问。 OAuth 2.0 提供一套 委托授权机制 :用户在云盘自己的页面确认授权,工具取得一个有权限范围和有效期的访问令牌,再用它调用云盘 API。用户的云盘密码不交给工具。 可以把它理解为:给工具一张“限定用途的通行证”。但令牌泄漏同...
学习
前端2026-10-15
前端动画全景指南
一、前端动画到底是什么 前端动画,就是让画面随时间、用户输入或页面状态连续变化。变化的对象可以是按钮的位置、弹窗的透明度、SVG 的线条、Canvas 里的粒子,也可以是三维场景中的镜头。 先把三个问题分开: | 问题 | 常见答案 | | | | | 画什么? | DOM/CSS、SVG、Canvas 2D、GPU 渲染的场景、预制动画素材 | | 怎样变化? | CSS 过渡/关键帧、Web Animations API、逐帧计算、...
学习
flutter2026-10-14
Flutter Android 正式签名实战:生成密钥、配置构建与验证 APK
Flutter Android 正式签名实战:生成密钥、配置构建与验证 APK Android 正式签名不需要购买证书。开发者可以自己生成密钥,用它给 APK 签名;密钥的用途是建立稳定的应用身份。签名有效并不等于应用已经通过商店审核,也不等于应用功能已完成真机验收。 本文面向 Flutter Android 项目,介绍从开发签名切换到独立正式签名的完整流程。应用名称、目录、包名和证书信息均使用通用示例,不包含真实项目密码、私钥、签名指...
学习
flutter2026-10-14
Flutter 新版本发布实战:完整安装包与 Shorebird 热更新
一、先判断该发完整包还是补丁 | 这次改动 | 发布方式 | | | | | Dart 文案、Flutter 页面布局、业务逻辑修复 | 通常可以发布补丁 | | 纯 Dart 依赖变化 | 可以尝试补丁构建,仍需验证兼容性 | | Kotlin、Java、Swift、Objective C 或原生构建配置变化 | 发布完整包 | | 插件的原生实现变化、新增原生插件、权限变化 | 发布完整包 | | 新增或修改打包图片、字体、应用图标...
学习
react2026-09-15
React 19.3 新特性总结
React 19.3 已发布到 npm,核心是把去年以实验 API 形式公布的 View Transitions 和 Fragment Refs 正式转正,同时新增了几个 DOM 与 RSC 相关的 API。下面按特性整理。 一、View Transitions(视图过渡动画,正式发布) 是什么 是基于浏览器原生 封装的组件。把你想要加动画的元素包起来,当元素 进入、退出、移动或尺寸变化 时,React 自动播放过渡动画。 基本用法 只...
学习

评论

请登录后发表评论

去登录
加载评论中...