-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript + CommonJS support #3
Comments
Hi @Jackman3005, Thanks a lot for reaching out! ProblemThe core problem is that Solution 1The best solution to this is to configure TypeScript to support ES modules, i.e. use I understand this is a big change and might not be worth it for you when trying to add a single library. However, this is a general problem you will encounter not only with CommonJS is deprecated by Node.js. Node 14 (the oldest Node.js version still officially supported) works natively with ES modules. For this reason, Solution 2 (details)The other option is to use a dynamic This solution has a problem though: The other problem with dynamic imports is that they are missing type information. This can be solved by using the following type assertion: Solution 2 (summary)
const ModernError = (await import('modern-errors')) as unknown as typeof import('modern-errors').default I have just added some documentation for the above here. Please let me know what you think. |
Hey @ehmicky thanks for getting back to me so quickly. I'll take a look at the options and see what works for us. Cheers! |
I guess part of the issue is I was not aware that "commonjs" is deprecated in Node 14. I got confused by the following in the Official Typescript Docs for Module field:
|
I notice other libraries I use actually provide both commonjs and esm files. What would the problem be for modern-errors to provide multiple module systems in the NPM package? I imagine I'm not the only one that isn't on ESM yet.. |
Please note I was referring to Node.js (not TypeScript) deprecating CommonJS. However the term "deprecating" was rather incorrect: Node.js is not deprecating nor planning to ever remove support for CommonJS. A more accurate way to put it would be: they are planning to focus primarily on ES modules for the present and future (see discussion about this). Double packagingI have tried double packaging in the past, and it turned out to be quite complicated for bigger projects like All my repositories use the same build logic. Changes in how one project is built impacts the other ones, so I tend to be cautious about major changes. I am currently supporting two different build steps: one for my JavaScript repositories (like This would also double the npm package size. While some users might elevate this with tree-shaking, many of them do not have a build setup that supports that. Beyond the complication of the build logic, supporting both CommonJS and ES modules in a bigger codebase can get quite tricky. The two module systems have subtle differences beyond
Some of the above differences have workarounds, but some are harder to fix. Another quite important issue would be: using dependencies which are pure ES modules, such as all libraries from Sindre Sorhus (which I rely on a lot). To be able to import those while still outputting CommonJS, dynamic CommonJS supportOn one hand, I do agree with you. Not everyone has migrated to ES modules yet, and I do want to support those users. It does bother me that some users might not be able to use my libraries due to using CommonJS. On the other hand, this would be too complicated to support. It also would prevent from using dependencies that are pure ES modules themselves (although some of those are critical to Updates on the solutionThe workarounds I was suggesting above have some problems unfortunately. First, they require using a top-level Then, they require Which means, for TypeScript users, it seems like there is no workaround, besides switching to ES modules. I updated the documentation accordingly. Thanks again for your interest in this project, and I'm sorry I could not find a solution for your setup. If switching to ES modules is an option for you, please check the following TypeScript guide which is quite thorough. This other guide is also very helpful. Doing that migration would not only allow you to use |
Unfortunately it looks like we will not be able to use your error framework for now due to the lack of double packaging/support for the CommonJS module system out of the box. It'd be great if we could more readily support the new module system in our code base or if the hacks to use it while we still are using CommonJS were less hacky. It looks great in concept and seems like something that would really clean up and improve our error handling system. One day I hope to be able to use it! |
Guidelines
Edit
button (pencil icon) and suggest a correction instead.Describe the bug
I am unable to import this library into my codebase. I think it is because the published version is not compiled to module system I can use (?).
It's quite possible there is something on my side that is causing the issue, or some configuration I could change to get it to work, but we already have > 80 other dependencies in our API that are not having any issues importing. (It's an express API)
Thanks in advance for any help, I'm quite excited to give this library a try!
Steps to reproduce
ts-node-dev
Configuration
tsconfig.json
Environment
Pull request (optional)
The text was updated successfully, but these errors were encountered: