Skip to content

Synchronous dynamic module loading? #19495

@jez9999

Description

@jez9999

In Node.JS the following pattern can be used to load optional dependencies:

try {
  var foo = require('foo')
  var fooVersion = require('foo/package.json').version
} catch (er) {
  foo = null
}
if ( notGoodFooVersion(fooVersion) ) {
  foo = null
}

// .. then later in your program ..

if (foo) {
  foo.doFooThings()
}

This doesn't work with an import foo = require() because import cannot be inside a try block. Dynamic imports have to be async, which unlike the above, requires the calling code to be async. How about having synchronous dynamic module importing which would allow an import inside a try block but load synchronously?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions