You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Next.js 13+TypeScript用Sendgrid时出现supports-color模块错误求助

解决方案

1. 锁定兼容的debug版本

最新版debug已转为ESM模块,但Sendgrid内部仍以CommonJS方式调用,导致模块冲突。安装兼容的CJS版本:

npm install debug@4.3.4 --save-exact

该版本依赖的supports-color为v7.x(CJS版本),可避免ESM导入错误。

2. 配置Next.js转译Sendgrid相关包

在next.config.js中添加transpilePackages,让Next.js将Sendgrid的CJS代码转译为适配框架的模块格式:

/** @type {import('next').NextConfig} */
const nextConfig = {
  transpilePackages: ['@sendgrid/mail', '@sendgrid/client', 'debug'],
}

module.exports = nextConfig

3. 清理缓存并重新安装依赖

执行以下命令彻底清理缓存与依赖,避免残留冲突:

rm -rf node_modules package-lock.json
npm install
npm run build # 验证问题是否解决

额外注意事项

  • 不要手动单独安装supports-color,让debug的依赖自动管理该包版本,避免版本不匹配。
  • 若使用App Router,确保Sendgrid的导入方式正确:
import sgMail from '@sendgrid/mail';

sgMail.setApiKey(process.env.SENDGRID_API_KEY!);

内容的提问来源于stack exchange,提问作者mkkl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 23:29:54