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

发布于:2025-03-20 ⋅ 阅读:(23) ⋅ 点赞:(0)

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 平台为例进行说明

# 查看当前版本
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管理的进程)命令,会得到以下提示:

>>>> 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即可。

$ 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

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

# windows 下安装 bun.js
powershell -c "irm bun.sh/install.ps1|iex"

# 升级
bun upgrade

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

示例代码

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

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}`);
});

运行进程

# 使用 node.js 运行
pm2 start web-node.mjs --interpreter node
# 使用 bun.js 运行
pm2 start web-bun.mjs --interpreter bun


运行4h后的状态:

运行7h后的状态:

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


网站公告

今日签到

点亮在社区的每一天
去签到