MaxNoetzold / y-mongodb-provider

Mongodb database adapter for Yjs
MIT License
51 stars 10 forks source link

"Yjs was already imported" notice #8

Closed cdolek closed 9 months ago

cdolek commented 11 months ago

Thank you for this package!

I'm testing the example provided in the readme, and I see the following on the console:

Yjs was already imported. This breaks constructor checks and will lead to issues! - https://github.com/yjs/yjs/issues/438

I also had to update the code as the following to make it work:

import { WebSocketServer } from "ws";
...
...
// y-websocket
const wss = new WebSocketServer({ server });

Using the following:

    "ws": "^8.14.2",
    "y-mongodb-provider": "^0.1.8",
    "y-websocket": "1.5.0",
    "yjs": "13.6.9"
MaxNoetzold commented 11 months ago

I'm testing the example provided in the readme, and I see the following on the console: Yjs was already imported. This breaks constructor checks and will lead to issues! - https://github.com/yjs/yjs/issues/438

Thank you. I also encounter this error when running my node app with type: module in package.json. However, since I typically bundle my projects for production, I haven't spent much time investigating this issue. It is well documented what this issue means, but I don't know yet how to fix it if this library is the problem. I might take the time to investigate in the future, but feel free to do it before me.

I also had to update the code as the following to make it work

I personally don't use this code, but I only wrote it as a quick example. You are absolutely right, in the current ws version, you have to import the Server like that. I will fix the README soon.

cdolek commented 11 months ago

Thank you @MaxNoetzold, I was able to resolve my issue by simply using a bundler (rollup)

MaxNoetzold commented 9 months ago

I made a basic websocket server with mdb persistence, and I'm facing the same problem discussed in this issue. I could bundle my server code, but I prefer to keep the project straightforward.

@raineorshine, any thoughts on fixing this issue?

I'm not entirely sure about the problem's specifics. I'm guessing y-mongodb-provider and the other yjs libraries might not be on the same page with the default module system (mjs or cjs). Any idea on how I can check this?

raineorshine commented 9 months ago

It's a really annoying issue.

The first step is to make sure all yjs dependencies in your tree are on the same version. I ran npm ls yjs on your backend project and they are all on v13.6.11 so that's not the problem.

Something else is causing yjs to be imported twice. I don't know the specific solution in your case, but you can learn more here: https://github.com/yjs/yjs/issues/438

MaxNoetzold commented 9 months ago

I don't know the specific solution in your case, but you can learn more here: yjs/yjs#438

Thank you. I looked into it before. I will rewrite my server to be written in commonjs just to check if this solves it. Honestly I think that my package.json of y-mongodb-provider defines the exports wrong but I don't really understand it anyways. Ill try to do it as esbuild recommends

MaxNoetzold commented 9 months ago

When I write the server as pure js commonjs node application it works totally fine.

So the problem has to be that the export of y-mongodb-provider as an esm module is wrong?

I'll test it this week on https://github.com/MaxNoetzold/y-mongodb-provider/tree/fix-esm-export. Looks promising

MaxNoetzold commented 9 months ago

Now actually solved with pr#12