在Nodejs的文件中:
此代码执行完全正常:
import { Client } from '@notionhq/client'
import { NotionCompatAPI } from 'notion-compat'
(async() => {
const token = 'your-token'
const notion = new NotionCompatAPI(
new Client({ auth: token })
)
const pageId = 'your-page-id'
const recordMap = await notion.getPage(pageId)
console.log(recordMap)
})()
但是我使用express作为nodejs后端框架在使用的时候会报错:
[ERROR] 14:41:09 Error: require() of ES Module path/node_modules/.pnpm/notion-compat@7.1.6_@notionhq+client@2.2.15/node_modules/notion-compat/build/index.js from path/src/notion/index.ts not supported.
Instead change the require of index.js in path/src/notion/index.ts to a dynamic import() which is available in all CommonJS modules.
具体代码文件: https://codesandbox.io/s/j67h8w
关键代码:
import { Client } from '@notionhq/client'
import { NotionCompatAPI } from 'notion-compat'
export const getBlocks = async(blockId?: string) => {
const token = 'your-token'
const notion = new NotionCompatAPI(
new Client({ auth: token })
)
const pageId = 'your-page-id'
const recordMap = await notion.getPage(pageId)
return recordMap
}