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

如何在Deno Fresh中导入Chart.js日期适配器库?

在Deno Fresh中为Chart.js配置日期适配器的问题

我正在使用Deno Fresh框架实现图表功能,为了在客户端渲染图表,我在islands/chart.tsx中编写了如下代码:

import { Chart } from "$fresh_charts/island.tsx";
export default Chart;

同时在routes/chart.tsx中使用对应代码后,基础图表可以正常渲染。

为了实现时间图表,我需要在routes/chart.tsx中导入日期适配器库。在Node环境中只需执行:

import 'chartjs-adapter-moment';

但在Deno中直接这样操作无法生效。

尝试1:添加npm: specifiers

我在routes/chart.tsx中添加:

import 'npm:chartjs-adapter-moment';

图表无法渲染。

尝试2:复制适配器源码

适配器代码较为简单,可修改后在Deno中运行,于是我将其复制到islands/chartjs-adapter-date-fns.js,并为导入模块添加npm: specifiers:

import { _adapters } from "npm:chart.js";
import { ... } from "npm:date-fns";

在routes/chart.tsx中导入该文件:

import "../islands/chartjs-adapter-date-fns.js";

图表仍无法渲染。

尝试3:使用date-fns Deno包

我在islands/chartjs-adapter-date-fns.js中修改导入路径:

import { _adapters } from "npm:chart.js";
import { ... } from "https://deno.land/x/date_fns@v2.22.1/index.js";

此时出现错误:Module not found "https://deno.land/x/date_fns@v2.22.1/getDay.js" at https://deno.land/x/date_fns@v2.22.1/nextDay/index.ts:2:20

尝试3.1:克隆date-fns/deno并修复路径

错误消失,但图表仍无法渲染。

尝试4:导入$fresh_charts/island.tsx

我将islands/chartjs-adapter-date-fns.js的导入语句修改为:

import { _adapters } from "$fresh_charts/island.tsx";
import { ... } from "../utils/deno/index.js";

此时出现错误:The requested module '$fresh_charts/island.tsx' does not provide an export named '_adapters'

请问有什么方法可以解决这个问题?


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 05:40:40