The documented CJS import doesn't work with typescript
See original GitHub issueHi, 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:
- Created 2 years ago
- Comments:21 (13 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I believe something like this can be written in the package.json to add typing automatically:
Instead of the current:
This would require creating an index.d.ts file, but tsc can do this automatically.
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.