跳至內容
Fumadocs

Node.js

在 Node.js 執行階段中存取內容。

設定

請使用 tsx 等 TypeScript 執行器來執行您的指令碼,因為範本中的 lib/source.ts 使用了省略副檔名的匯入與路徑別名,而 Node.js 無法解析這些寫法。

scripts/example.ts
import { register } from 'fumadocs-mdx/node';

// 註冊 Node.js 載入器
register();

// 存取內容
const { source } = await import('../lib/source');
console.log(source.getPages());
npx tsx scripts/example.ts

此載入器只會轉換 ES 模組。請在 package.json 中設定 "type": "module",讓 lib/source.ts 以 ES 模組的形式載入。此設定會套用到套件內所有的 .js 檔案,因此請先將 next.config.js 等 CommonJS 檔案轉換為 ESM。

register() 函式可接受以下選項:

register({
  configPath: '...',
  // ...
});

How is this guide?

最後更新於