IT工具 | node.js 进程管理工具 PM2 大升级!支持 Bun.js

P(rocess)M(anager)2 是一个 node.js 下的进程管理器,内置负载均衡,支持应用自动重启,常用于生产环境运行 node.js 应用,非常好用👍

🌼概述

2025-03-15日,PM2发布最新版本v6.0.5,这是一个大版本升级(上一版本v5.4.2发布于24年7月),本次更新带来了 Bun.js 的支持🎉。

  • 支持 Bun.js 运行时
  • 默认禁用 git 解析
  • PM2 serve 响应增加 WEBP 内容类型

⬆️版本升级

此处以 windows 平台为例进行说明

bash 复制代码
# 查看当前版本
pm2 -v
# 5.3.0

# 直接通过以下命令覆盖安装
npm i -g pm2
# added 13 packages, removed 63 packages, and changed 121 packages in 1m
# 13 packages are looking for funding
#   run `npm fund` for details

升级后,我们执行 pm2 ls(显示pm2管理的进程)命令,会得到以下提示:

bash 复制代码
>>>> In-memory PM2 is out-of-date, do:
>>>> $ pm2 update
In memory PM2 version: 5.3.0
Local PM2 version: 6.0.5

我们执行pm2 update即可。

bash 复制代码
$ pm2 update
                  -------------
  ██████╗ ███╗   ███╗██████╗     ██╗    ██╗ ██████╗
  ██╔══██╗████╗ ████║╚════██╗    ██║   ██╔╝██╔═══██╗
  ██████╔╝██╔████╔██║ █████╔╝    ██║  ██╔╝ ██║   ██║
  ██╔═══╝ ██║╚██╔╝██║██╔═══╝     ██║ ██╔╝  ██║   ██║
  ██║     ██║ ╚═╝ ██║███████╗    ██║██╔╝   ╚██████╔╝
  ╚═╝     ╚═╝     ╚═╝╚══════╝    ╚═╝╚═╝     ╚═════╝
                  https://pm2.io/
     Harden your Node.js Production Environment

      - Real-time Monitoring Web Interface
      - Pro Active Alerting System
      - Production Profiling for Memory and CPU
      - PM2 Runtime High Availability Fallback

             Start using it by typing:

                    $ pm2 plus
                  -------------

[PM2][WARN] No process found
[PM2] [v] All Applications Stopped
[PM2] [v] PM2 Daemon Stopped
[PM2] Spawning PM2 daemon with pm2_home=C:\Users\admin\.pm2
[PM2] Restoring processes located in C:\Users\admin\.pm2\dump.pm2
>>>>>>>>>> PM2 updated

🛠️小试牛刀

安装 Bun.js

如果您已经安装,请跳过😄

bash 复制代码
# windows 下安装 bun.js
powershell -c "irm bun.sh/install.ps1|iex"

# 升级
bun upgrade

# 卸载
powershell -c ~\.bun\uninstall.ps1

示例代码

我们让 deepseek 写一段简单的 WEB 服务

js 复制代码
import http from 'http';
import url from 'url';

// 记录程序启动时间
const startTime = Date.now();

// 创建 HTTP 服务器
const server = http.createServer((req, res) => {
    const parsedUrl = url.parse(req.url, true);

    // 处理 /status 路由
    if (parsedUrl.pathname === '/status') {
        // 计算程序已经运行的时间(秒)
        const uptime = Math.floor((Date.now() - startTime) / 1000);

        // 返回 JSON 响应
        res.writeHead(200, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify({ uptime }));
    } else {
        // 处理其他路由
        res.writeHead(404, { 'Content-Type': 'text/plain' });
        res.end('Not Found');
    }
});

// 监听端口
const PORT = 3000;
server.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});

运行进程

bash 复制代码
# 使用 node.js 运行
pm2 start web-node.mjs --interpreter node
# 使用 bun.js 运行
pm2 start web-bun.mjs --interpreter bun

运行4h后的状态:

运行7h后的状态:

Bun.js 跑的应用占用的内存越来越低?😂

相关推荐
Moment18 分钟前
Next.js 15.5 带来 Turbopack Beta、Node 中间件稳定与 TypeScript 强化 🚀🚀🚀
前端·javascript·react.js
yzzzzzzzzzzzzzzzzz40 分钟前
初识javascript
前端·javascript
你的人类朋友10 小时前
【Node&Vue】JS是编译型语言还是解释型语言?
javascript·node.js·编程语言
烛阴10 小时前
TypeScript高手密技:解密类型断言、非空断言与 `const` 断言
前端·javascript·typescript
样子201810 小时前
Uniapp 之renderjs解决swiper+多个video卡顿问题
前端·javascript·css·uni-app·html
黑客飓风11 小时前
JavaScript 性能优化实战大纲
前端·javascript·性能优化
YeeWang14 小时前
🎉 Eficy 让你的 Cherry Studio 直接生成可预览的 React 页面
前端·javascript
gnip14 小时前
Jenkins部署前端项目实战方案
前端·javascript·架构
Orange30151114 小时前
《深入源码理解webpack构建流程》
前端·javascript·webpack·typescript·node.js·es6
李明卫杭州15 小时前
CSS `clamp()` 函数详解
javascript