Node.js 节点画布为registerFont将.ttf文件导入TS

Node.js 节点画布为registerFont将.ttf文件导入TS,node.js,typescript,fonts,node-canvas,Node.js,Typescript,Fonts,Node Canvas,我想在节点画布创建的画布中使用本地字体ttf格式 我已经创建了一个打字文件并将其添加到我的tsconfig中: 字体d.ts 导入并注册我的字体: 字体/索引.ts 组件技术 但是,当代码运行时,我得到错误: errorType:Runtime.ImportModuleError,errorMessage:Error:找不到模块'./comic sans.ttf' 我正在使用Typescript和NodeJS。这种导入字体的方式是否错误 我保证,我再也不会使用Comic SAN了。您可以直接在r

我想在节点画布创建的画布中使用本地字体ttf格式

我已经创建了一个打字文件并将其添加到我的tsconfig中:

字体d.ts

导入并注册我的字体:

字体/索引.ts

组件技术

但是,当代码运行时,我得到错误:

errorType:Runtime.ImportModuleError,errorMessage:Error:找不到模块'./comic sans.ttf'

我正在使用Typescript和NodeJS。这种导入字体的方式是否错误


我保证,我再也不会使用Comic SAN了。

您可以直接在registerFont函数中导入字体,如下所示:

registerFont'./comic sans.ttf',{family:'comic sans'

declare module '*.ttf';
import * as Font_ComicSans from './comic-sans.ttf';

const Fonts = {
    Font_ComicSans
};

export default Fonts;

import Fonts from '../fonts';
import { registerFont } from 'canvas';
...
registerFont(Font_ComicSans, { family: 'Comic Sans' });