-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
It is very difficult to call the import
function from a CommonJS module.
#52775
Comments
Thank you. Do you know the magic combination of The closest I came was with: "module": "CommonJS",
"moduleResolution": "node16", But that doesn't work correctly with
|
@Jason3S |
Thank you, that is clear. It is really just a short coming with It seems like It works with importing modules that are project:
|
I believe in general, if you're using |
Sorry for the confusion. There are two
|
@Jason3S it’s impossible to say without an actual example here, but chances are that the package you’re trying to import is configured incorrectly, such that TypeScript is directed to ESM types even while Node is directed to a CJS module. You can check with https://arethetypeswrong.github.io. |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow or the TypeScript Discord community. |
Thank you for the link, I didn't realize it existed. I investigated it over the weekend and discovered that it is NOT possible using only TypeScript to create dual output ESM / CJS library modules (if it is, I would love to see an example). A tool to correctly rename the ESM TypeScript has help lift the entire JavaScript ecosystem. It is a very nice language and a joy to work with. Thank you for helping with that. When working on #52593, some good examples how to correctly export types for both CJS and ESM would be very useful. Like what VS Code does for extensions (vscode-extension-samples). |
Bug Report/Feature Request
tldr;
The fix was to use:
tsconfig.json
Description
I did search for possible duplicates of this issue, but I wasn't able to filter the issues enough to find them.
Feature Request: flag to enable the
import()
function.Bug: the transpiler converts
import()
function intorequire()
in.cjs
files.It seems impossible to call the
import()
function from a TypeScript file compiling to CommonJS. It always gets converted into an inlinerequire()
.It is going to take a long time for code bases to migrate from CommonJS to ESM. In the mean time, how can we make it possible to easily asynchronously import an ESM modules on targets that support it? I don't want to get into the ESM/CommonJS debacle. My goal here is to just to async import an ESM Module from CommonJS TypeScript.
Even if we cannot get TypeScript to work, a bit of JavaScript should work, but it does not.
dynamicImport.cjs
dynamicImport.d.cts
If I add that code into my CommomJS TypeScript project with
allowJs
,import
gets converted intorequire
, even if the target is ES2020.dist/dynamicImport.cjs
I realize that it is possible through some creative uses of
tsconfig
and"composite": true
to get this to transpile correctly. But, that is missing the point. How to make dynamic imports easy?🔎 Search Terms
import, function, commonjs, allowjs, javascript
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
🙂 Expected behavior
The text was updated successfully, but these errors were encountered: