# optional imports using import attributes

**URL:** <https://es.discourse.group/t/optional-imports-using-import-attributes/2385>\
**Category:** 💡 Ideas\
**Created:** [June 17, 2025, 1:22pm UTC](https://es.discourse.group/t/optional-imports-using-import-attributes/2385 "2025-06-17T13:22:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dotcarmen](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/dotcarmen/32/2483_2.png) [@dotcarmen](https://es.discourse.group/u/dotcarmen)\
**Post date:** [June 17, 2025, 1:22pm UTC](https://es.discourse.group/t/optional-imports-using-import-attributes/2385/1 "2025-06-17T13:22:45Z")

</div>

Contrary to [this optional import idea](https://es.discourse.group/t/optional-imports/1628), I'd like to support optional imports without introducing any new syntax. This idea is similar to [weak imports](https://es.discourse.group/t/weak-imports/1629) by the same author, the discussion of which has gone stale and upstream proposals have since merged with changes. However, the latter proposal uses syntax proposed from the former, which I believe is unnecessary.

Since [import attributes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import/with) are now part of the ES spec, I'd like to propose behavior for the `optional` attribute: if the resolution of the specified module fails, then the imported value(s) are `undefined`. This is intended to supplement the somewhat common usage of optional dependencies in the npm ecosystem and [`where` attributes on `<script>`s](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/script/type/speculationrules#where).

As an example, say your file optionally depends on `foo`. Currently, the only way I'm aware of importing optional modules requires use of ~~gross~~ `require()` in a `try` block:

```js
var foo = undefined;
try {
  foo = require("foo").default;
} catch {
  console.debug("module `foo` is unavailable");
}

```

with proposed behavior of the `optional` import attribute, then the above code can be simplified to:

```js
import foo from "foo" with { optional: true };

```

Since no new syntax is introduced, the language stays simple, and the above code does what is obvious: if `"foo"` is successfully resolved, then `foo` is the imported module, otherwise it's `undefined`.

In the other linked discussions, users wanted to be able to handle other helpful cases like fallbacks, but I think think the lack of new syntax in this proposal should mean that other desirable features are out of scope. if a user wants to fallback, then they can use `??` in the rest of the file to handle this. for example:

```js
import { Foo } from "foo" with { optional: true };
import { BackupFoo } from "backup-foo";

const MyFoo = Foo ?? BackupFoo;

```

I also believe this specification is tree-shakeable, since it can be known before runtime whether `Foo` is resolved or not, though I'm not intimately familiar with tree shaking to know for sure under what circumstances tree-shaking fails.
