DEV Community

Mark Leo
Mark Leo

Posted on

Please help with `[ERROR] 14:41:09 Error: require() of ES Module not supported.`

In a Node.js file:
This code executes perfectly fine:

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)

})()
Enter fullscreen mode Exit fullscreen mode

However, when I use Express as the Node.js backend framework, I encounter an error:

[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.
Enter fullscreen mode Exit fullscreen mode

Specific code file: https://codesandbox.io/s/j67h8w

Key code:

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
}
Enter fullscreen mode Exit fullscreen mode

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs