我希望使用 Google Cloud Functions 与 Notion API 进行交互。我开始从他们的 API 文档中提取第一个示例并将其放入 CF(见下文)。但是我收到以下错误日志:
Detailed stack trace: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /workspace/index.js
我在尝试在本地运行 Notion API 时收到了类似的消息,但能够通过更改 import { Client } from "@notionhq/client"
为 import notion from "@notionhq/client"; const Client = notion
. 由于某种原因,该修复程序在云功能中不起作用。
我还阅读了其他 SO 答案,我需要将 package.json 中的类型更改为模块,但我已经这样做了,问题仍然存在。
index.js
import { Client } from "@notionhq/client"
exports.notionBot = (req, res) => {
let message = req.query.message || req.body.message || 'Testing the notion API with Cloud Functions!';
const notion = new Client({ auth: process.env.NOTION_KEY })
const databaseId = process.env.NOTION_DATABASE_ID
async function addItem(text) {
try {
await notion.request({
path: "pages",
method: "POST",
body: {
parent: { database_id: databaseId },
properties: {
title: {
title:[
{
"text": {
"content": text
}
}
]
}
}
},
})
console.log("Success! Entry added.")
} catch (error) {
console.error(error.body)
}
}
addItem("Yurts in Big Sur, California")
res.status(200).send(message);
};
包.json
{
"name": "notion-example",
"type": "module",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"@notionhq/client": "^0.1.9",
"esm": "^3.2.25"
}
}