question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The documented CJS import doesn't work with typescript

See original GitHub issue

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).

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
christiaangoossenscommented, Oct 12, 2022

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.

0reactions
aricartcommented, Feb 10, 2022

will do - but realize again the old create app is not supported anymore, and yes tsc and bundlers have received updates. Going the other way is not correct - I believe that if you do the “expanded” import where you point it at the file, it will do the right thing for you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - ECMAScript Modules in Node.js - TypeScript
This code works in CommonJS modules, but will fail in ES modules because relative import paths need to use extensions. As a result,...
Read more >
Allow ES Module Type-Only Imports from CJS Modules #49721
Sometimes, we only want to import the types from a package. Typescript has the very helpful import type directive to acommodate this use...
Read more >
Typescript: Cannot use import statement outside a module
I use this construction: import { Class } from 'abc'; When i run the code, i have this error: Cannot use import statement...
Read more >
CommonJS modules | Node.js v19.3.0 Documentation
Calling import() always use the ECMAScript module loader. Accessing the main module#. When a file is run directly from Node.js, require.main is ...
Read more >
How we employed the new ECMAScript Module Support in ...
Typescript changes the import depending on your target and module definition in tsconfig (see the typescript documentation).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found