Highcharts Export Server通过PM2启动报错ERR_REQUIRE_ESM求助
解决PM2启动Highcharts Node-Export-Server v4.0.2的ESM兼容问题
问题原因
Highcharts node-export-server v4.0.2采用ESM模块规范,而PM2默认的进程容器(ProcessContainerFork)使用CommonJS的require()加载模块,两者不兼容导致报错。
解决方案
方案1:直接指定Node.js作为解释器运行CLI文件
跳过PM2的CommonJS容器,让Node.js直接执行ESM模块:
pm2 start /var/www/.nvm/versions/node/v20.15.1/lib/node_modules/highcharts-export-server/bin/cli.js --interpreter node -- --enableServer 1 --port 8000 --noDownload 0
通过--interpreter node参数,让PM2直接调用Node.js运行cli.js,避免PM2自身用require()加载ESM模块的冲突。
方案2:创建ESM启动脚本
- 在项目目录新建
start-export-server.mjs文件:
import { spawn } from 'child_process'; // 调用highcharts-export-server的CLI命令 const child = spawn('highcharts-export-server', ['--enableServer', '1', '--port', '8000', '--noDownload', '0'], { stdio: 'inherit' }); child.on('exit', (code) => { process.exit(code); });
- 用PM2启动该脚本:
pm2 start start-export-server.mjs --interpreter node
方案3:不推荐的兼容方式
- 尝试降级PM2到支持ESM加载的版本,但可能引发其他依赖兼容性问题;
- 修改highcharts-export-server包内
package.json的type字段为commonjs,但会破坏模块本身的ESM规范,不建议操作。
验证
执行pm2 list查看进程状态,若显示online则启动成功,可通过访问端口8000测试导出功能。
内容的提问来源于stack exchange,提问作者Marian Ioan
相关产品推荐
相关产品推荐

