Support a way to use dynamic import expressions within module type commonjs #45125
Closed
5 tasks done
Labels
Needs Investigation
This issue needs a team member to investigate its status.
Milestone
Suggestion
These days - nodejs supports esm/commonjs interop by allowing dynamic import expressions inside of cjs modules. Currently, this is the only way to import/require pure ESM packages inside of commonjs based projects. Refactoring a project to pure ESM is not always possible. Some popular open source maintainer are forcing use of pure ESM (ie.. @sindrsorhus)
🔍 Search Terms
module modules import require commonjs esm esm interop export default exports await import import( import() dynamic imports
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
Add an escape hatch - perhaps by a comment flag or configuration option to allow dynamic imports in project compiling to commonjs.
📃 Motivating Example
Set compilerOptions.module to "commonjs"
case:1
// error
import meow from 'meow';
case:2
// currently an error
(async () => { const meow = await import('meow') });
compiles to something like
const meow = await Promise.resolve(require('meow'))
// after adding a new option to allow dynamic imports case2 would not transform dynamic import expressions while import statements would compile to require.
(async () => { const meow = await import('meow') });
💻 Use Cases
This would allow to use TS using current Node.JS capabilities and native modules more naturally as well as support more libraries for NodeJs without having to use something like webpack to achieve.
The text was updated successfully, but these errors were encountered: