Skip to content

useValidNamespaceImports (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useValidNamespaceImports": "error"
}
}
}
}

Require that names read from a namespace import are exported by the imported module.

A namespace import, such as import * as utils from "./utils.js", puts every export of a module into one object. Reading a name that the module doesn’t export gives undefined, which usually means the name has a typo, or the export was renamed or removed.

This rule reports:

  • reading a name from a namespace import that the imported module doesn’t export, including names read through destructuring, such as const { name } = utils, and JSX elements, such as <Components.Button />;
  • assigning a value to a member of a namespace import, which throws an error at runtime because namespace imports are read-only;
  • namespace imports and export * as name from statements of a module that has no exports.

The rule also follows namespaces that a module exports, such as export * as helpers from "./helpers.js", so utils.helpers.missing is checked as well.

The rule only checks modules that use import or export statements. It skips CommonJS modules and TypeScript modules that use export =, because their exports can’t be determined reliably.

Note that if you use TypeScript, you probably don’t need this rule, since TypeScript already reports these mistakes.

utils.js
export const add = (a, b) => a + b;
invalid-read.js
import * as utils from "./utils.js";
utils.subtract(2, 1);
/invalid-read.js:3:7 lint/nursery/useValidNamespaceImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ The namespace utils has no export named subtract.

1 │ import * as utils from "./utils.js";
2 │
> 3 │ utils.subtract(2, 1);
│ ^^^^^^^^
4 │

ℹ Reading a name that a module doesn't export gives undefined.

ℹ Check the spelling of the name, or export it from the module.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

invalid-destructuring.js
import * as utils from "./utils.js";
const { subtract } = utils;
/invalid-destructuring.js:3:9 lint/nursery/useValidNamespaceImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ The namespace utils has no export named subtract.

1 │ import * as utils from "./utils.js";
2 │
> 3 │ const { subtract } = utils;
│ ^^^^^^^^
4 │

ℹ Reading a name that a module doesn't export gives undefined.

ℹ Check the spelling of the name, or export it from the module.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

invalid-assignment.js
import * as utils from "./utils.js";
utils.add = () => 0;
/invalid-assignment.js:3:1 lint/nursery/useValidNamespaceImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ The members of the namespace utils can't be reassigned.

1 │ import * as utils from "./utils.js";
2 │
> 3 │ utils.add = () => 0;
│ ^^^^^^^^^
4 │

ℹ Namespace imports are read-only, so this assignment throws an error at runtime.

ℹ To change a value that a module exports, export a function from that module that changes it.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

utils.js
export const add = (a, b) => a + b;
valid.js
import * as utils from "./utils.js";
utils.add(2, 1);
const { add } = utils;