TypeCellOS / BlockNote

A React Rich Text Editor that's block-based (Notion style) and extensible. Built on top of Prosemirror and Tiptap.
https://www.blocknotejs.org/
Mozilla Public License 2.0
6.31k stars 434 forks source link

Error [ERR_REQUIRE_ESM]: require() of ES Module #1008

Closed odufuwa-segun closed 2 weeks ago

odufuwa-segun commented 4 weeks ago

Describe the bug Error when starting nodejs application

Screenshot 2024-08-14 at 2 55 01 PM

node_modules/ts-node/dist/index.js:851 return old(m, filename); ^ Error [ERR_REQUIRE_ESM]: require() of ES Module node_modules/@blocknote/core/node_modules/rehype-parse/index.js from node_modules/@blocknote/core/dist/blocknote.umd.cjs not supported. Instead change the require of index.js in node_modules/@blocknote/core/dist/blocknote.umd.cjs to a dynamic import() which is available in all CommonJS modules. at require.extensions. [as .js] (node_modules/ts-node/dist/index.js:851:20) at node_modules/@blocknote/core/dist/blocknote.umd.cjs:1:99 at Object. (node_modules/@blocknote/core/dist/blocknote.umd.cjs:1:2397) at require.extensions. [as .js] (node_modules/ts-node/dist/index.js:851:20) at node_modules/@blocknote/server-util/dist/blocknote-server-util.umd.cjs:1:70 at Object. (node_modules/@blocknote/server-util/dist/blocknote-server-util.umd.cjs:1:487) at require.extensions. [as .js] (node_modules/ts-node/dist/index.js:851:20) at Object. (src/yjs.ts:8:23) at m._compile (node_modules/ts-node/dist/index.js:857:29) at require.extensions. [as .ts] (node_modules/ts-node/dist/index.js:859:16) at Object. (src/socket.ts:4:18) at m._compile (node_modules/ts-node/dist/index.js:857:29) at require.extensions. [as .ts] (node_modules/ts-node/dist/index.js:859:16) at Object. (src/index.ts:32:21) at m._compile (node_modules/ts-node/dist/index.js:857:29) at require.extensions. [as .ts] (node_modules/ts-node/dist/index.js:859:16) at phase4 (node_modules/ts-node/dist/bin.js:466:20) at bootstrap (node_modules/ts-node/dist/bin.js:54:12) at main (node_modules/ts-node/dist/bin.js:33:12) at Object. (node_modules/ts-node/dist/bin.js:579:5) { code: 'ERR_REQUIRE_ESM' } [nodemon] app crashed - waiting for file changes before starting... Error [ERR_REQUIRE_ESM]: require() of ES Module

To Reproduce Create Node app with package.json and tsconfig.json

{ "name": "App", "version": "0.0.1", "description": "Collaboration Provider Service(Powered by SocketIO & YJS)", "type": "commonjs", "devDependencies": { "@babel/preset-typescript": "^7.22.5", "@types/html-to-text": "^9.0.4", "@types/jest": "^29.5.2", "@types/node": "^20.8.6", "jest": "^29.5.0", "nodemon": "^3.0.1", "swagger-autogen": "^2.23.7", "ts-jest": "^29.1.0", "ts-node": "10.9.1", "tspec": "^0.1.112", "typescript": "^5.2.2" }, "dependencies": { "@blocknote/server-util": "^0.15.3", "@logtail/node": "^0.4.21", "@sentry/node": "^7.103.0", "@socket.io/redis-adapter": "^8.2.1", "@socket.io/sticky": "^1.0.4", "cors": "^2.8.5", "dotenv": "^16.1.4", "express": "^4.17.2", "html-to-text": "^9.0.5", "mysql2": "^2.3.3", "redis": "^4.6.13", "reflect-metadata": "^0.1.13", "sha1": "^1.1.1", "socket.io": "^4.7.2", "typeorm": "0.3.7", "y-socket.io": "^1.1.3" }, "scripts": { "build": "npm run clean && tsc --project ./", "dev": "npm run clean && nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec 'ts-node' src/index.ts", "start": "node build/index.js", "test": "jest" } } { "compilerOptions": { "lib": [ "es6" ], "target": "es2018", "module": "commonjs", "moduleResolution": "node", "outDir": "./build", "emitDecoratorMetadata": true, "experimentalDecorators": true, "sourceMap": true, "skipLibCheck": true, "esModuleInterop": true, "allowImportingTsExtensions": true }, "exclude": [ "node_modules" ] }

src/index.tsx ` import "reflect-metadata" import express, {NextFunction, Request, Response} from "express" import http, {Server} from "http"; import {ServerBlockNoteEditor} from "@blocknote/server-util";

const app = express() const server = http.createServer(app);

const defaultBlock: any = [{ type: "paragraph", content: "", }]

app.get('/', (req: Request, res: Response, next: NextFunction) => {

    const editor = ServerBlockNoteEditor.create();
    editor.blocksToYXmlFragment(defaultBlock, doc.getXmlFragment("document-store"))

return response.send('Huh? works..')

})

const startListening = (server: Server, port: number) => { server.listen(port)

console.log(`Servidor iniciado on port: 3030`);
console.log(`Server started: ${new Date()}`);

} `

Note: Code is stripped down for simplicity.

Misc

YousefED commented 4 weeks ago

As we define type: "module" in package json I think nodejs should load the ES module (blocknote.js instead of blocknote.umd.cjs). So this seems to be going wrong.

Can you check whether you still have this issue with pure node? (I see you're using ts-node)

odufuwa-segun commented 4 weeks ago

@YousefED thank you for the response. I still have it when running with pure node after build.

odufuwa-segun commented 4 weeks ago

@YousefED I reproduced it on Stackblitz https://stackblitz.com/edit/stackblitz-starters-zvg6g4

odufuwa-segun commented 4 weeks ago

Update!!! I was able to fix it by replacing ts-node(in development) and node(in production) with tsx.

I also updated my tsconfig.json file, changing compileOptions.target and compileOptions.module to es2022