Skip to content

with

In JavaScript, the with keyword starts a legacy statement that adds an object’s properties to the scope of the code inside it, and it also introduces the import attributes clause in module syntax.

The with statement makes every unqualified name in its body check the object’s properties first. That hides which binding a name refers to, so the statement is a SyntaxError in strict mode, including in ES modules and class bodies. Destructuring is the usual replacement.

Import attributes follow the module specifier in an import declaration or an export … from declaration, and tell the host how to load the module. The standard attribute is type, and { type: "json" } loads a JSON file as a module. Import attributes were added in ES2025. They’re Baseline Newly available since 2025, and supported in Node.js 20.10 and later.

JavaScript with Keyword Examples

The with statement, which runs only in sloppy mode, adds an object’s properties to the scope of a block:

Language: JavaScript
> const point = { x: 3, y: 4 };
undefined
> with (point) {
…   Math.sqrt(x * x + y * y);
… }
5

Inside the block, x and y resolve to point.x and point.y. The same statement in an ES module fails before any code runs:

Language: JavaScript Filename: legacy.mjs
with (Math) {
  console.log(PI);
}

Node.js reports the error when it loads the module:

Language: Shell
$ node legacy.mjs
SyntaxError: Strict mode code may not include a with statement

With import attributes, a module can import this settings.json file:

Language: JSON Filename: settings.json
{ "theme": "dark", "fontSize": 14 }

The type: "json" attribute makes Node.js parse the file as JSON, and the parsed object becomes the default export:

Language: JavaScript Filename: theme.mjs
import settings from "./settings.json" with { type: "json" };

console.log(`Theme: ${settings.theme}`);
console.log(`Font size: ${settings.fontSize}`);

Running theme.mjs prints both settings:

Language: Shell
$ node theme.mjs
Theme: dark
Font size: 14

Without the attribute, Node.js throws a TypeError. Dynamic import() takes the attributes as a with option.

JavaScript with Keyword Use Cases

  • Importing JSON files as modules with { type: "json" }
  • Loading JSON data through dynamic import() with the with option
  • Recognizing the legacy with statement in old sloppy-mode scripts

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 3, 2026