nats.ws: The documented CJS import doesn't work with typescript

Hi, the docs start with saying that CommonJS version is rooted at “./cjs/nats.js”, however, that cannot be imported directly from TypeScript, as there are no typings.

For CommonJS + TypeScript, the only import I got to work was: import {connect, NatsConnection} from "nats.ws/lib/src/mod.js";

Is that the correct way?

Plus obviously it would be good to fix the basics – use a more standard package layout that is easy and obvious to import, and document clearly how to import for modern stuff for those who still fail at the easy and obvious (currently the “importing the module” section of the docs only talks about using a <script> tag, which admittedly was a great and popular way in the 1990s and even early 2000s, but in 2022 not so much anymore).

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 21 (13 by maintainers)

Most upvoted comments

I believe something like this can be written in the package.json to add typing automatically:


  "exports": {
    ".": {
      "import": {
        "types": "./lib/src/index.d.ts",
        "default": "./esm/nats.js"
      },
      "require": {
        "types": "./lib/src/index.d.ts",
        "default": "./cjs/nats.js"
      }
    }
  },

Instead of the current:

"exports": {
  ".": {
    "require": "./cjs/nats.js",
    "import": "./esm/nats.js"
  }
},

This would require creating an index.d.ts file, but tsc can do this automatically.