import/unambiguous

Require a module to have an import or export so it cannot be read as a script

Also called import-x/unambiguous

A file with no import and no export is valid both as a script and as a module, and the two run differently. A module is in strict mode, its top-level this is undefined, and its top-level declarations aren't global. A tool that has to guess from the content can guess wrong.

One import or export statement settles it. import(), import.meta, top-level await and require() don't count.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"import/unambiguous" = "error" # or "warn"

Examples#

example.js1 problem
function greet(name) {
  console.log(`Hello, ${name}`);
}

greet("Ada");
example.js1 problem
const { greet } = await import("./greet.js");

greet("Ada");
example.jsNo problems
export function greet(name) {
  console.log(`Hello, ${name}`);
}

If the file has nothing to export, an empty export marks it as a module:

example.jsNo problems
function greet(name) {
  console.log(`Hello, ${name}`);
}

greet("Ada");

export {};

A .cjs file is never a module, so it isn't checked:

greet.cjsNo problems
console.log("Hello, Ada");

When not to use it#

If every tool you use decides by the file extension or by "type" in package.json, the file's content doesn't matter and this rule is noise.