How to use y-protocol with typescript

Implementing a websocket-server on which a y-websocket provider can rely requires using lib0 and y-protocols.

y-protocols exposes esm modules (i’m not sure if that’s the right term) for awareness and sync with type-definition, while the example y-websocket server uses the y-protocols/dist/*.cjs libs. The esm modules give error when I try to run the code compiled with typescript

The y-protocols/dist/*.cjs libs have no type definitions, which makes it impossible to use them in a typescript setup (and I cannot give up typescript).

How can I get started with building a typescript websockets server that is horizontally scalable, persists y-js documents, and which y-websocket can connect to as a provider while maintaining proper typescript typings?

Instead of importing lib0/encoding.js, you can import lib0/encoding. This should give you typings and automatic module resolution to the cjs files if you are running a current node version.

  • upgrade to node >=v14.7
  • use lib0/encoding instead of lib0/encoding.js

I am already doing that, you can check the code over here

The code compiles well and good. But when I try to run the file, I get the following errors:

My node version is 14.16.0

Please help

I think I have things working now. Had to convert everything to use esModules and then deal with the technicalities that come with that. Thanks