Modern JavaScript applications work with more than JavaScript and TypeScript files. Projects frequently import JSON configuration, localization resources, WebAssembly modules, and other non-JavaScript resources. As applications grow, developers need a consistent way to tell the JavaScript runtime how an imported resource should be interpreted.
TypeScript supports ECMAScript import attributes, which provide additional information alongside an import statement. They use the with keyword and allow developers to specify attributes such as the expected module type.
For example, importing a JSON file in an ES module can look like this:
import config from "./config.json" with { type: "json" };
console.log(config);
The import attribute tells the runtime that the imported resource must be treated as JSON. This is especially important in Node.js, where the JSON module type attribute is mandatory.
This article explains how import attributes work in TypeScript, how to configure a project, how to use them with static and dynamic imports, and what developers should consider when using them in Node.js and browser-based applications.
What Are Import Attributes?
Import attributes are part of the ECMAScript module syntax. They allow developers to attach metadata to an import declaration so that the runtime or module-loading environment can understand how a resource should be handled.
The syntax uses the with keyword followed by an object containing one or more attributes.
import data from "./data.json" with { type: "json" };
In this example:
- The import statement identifies the resource.
- The
with keyword introduces the import attributes. - The type attribute specifies that the resource should be interpreted as JSON.
Import attributes are useful because a file extension alone does not always provide enough information about how a resource should be loaded. The runtime can use the attribute to validate the resource type and apply the appropriate loading behavior.
TypeScript 5.3 introduced support for the modern import attributes syntax. TypeScript understands the syntax and preserves the attributes for the runtime or another tool in the build process. It does not independently implement the resource-loading behavior.
This distinction matters: TypeScript can check and compile the import statement, but the actual support for a particular attribute depends on the environment that loads the module.
Import Attributes vs. Import Assertions
Earlier versions of the proposal used the assert keyword. Developers may still encounter examples written with import assertions in older tutorials and repositories.
The older syntax looked like this:
import config from "./config.json" assert { type: "json" };
The modern syntax replaces assert with with:
import config from "./config.json" with { type: "json" };
The change is not simply a formatting preference. Import assertions were replaced by the import attributes proposal, and modern TypeScript and Node.js documentation use the with syntax.
For new projects, use import attributes instead of writing new code with assert.
| Feature | Import assertions | Import attributes |
|---|
| Syntax keyword | assert | with |
| Modern syntax | No | Yes |
| TypeScript support | Legacy compatibility depends on compiler mode | Supported since TypeScript 5.3 |
| Recommended for new code | No | Yes |
Configuring TypeScript for Import Attributes
The correct TypeScript configuration depends on where the application runs.
For Node.js applications, use a Node-aware module configuration. For example, a project targeting modern Node.js can use the following tsconfig.json:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"outDir": "dist"
},
"include": ["src"]
}
The important settings are module and moduleResolution. They tell TypeScript to follow Node.js module resolution and module-format rules.
The NodeNext configuration follows the current Node.js module behavior. If you are targeting a specific Node.js version, choose a module mode that reflects the features supported by that version.
For a bundler-based application, a different configuration may be appropriate:
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "Bundler",
"strict": true,
"resolveJsonModule": true,
"noEmit": true
},
"include": ["src"]
}
The bundler is responsible for resolving and processing imported resources. Check its documentation to confirm that it supports the attributes used by your application.
Importing JSON Files in TypeScript
JSON imports are one of the most common use cases for import attributes.
Consider a project that stores application settings in a JSON file.
The config.json file contains:
{
"applicationName": "Developer Portal",
"environment": "production",
"apiUrl": "https://api.example.com",
"enableLogging": true
}
You can import it using the JSON type attribute:
import config from "./config.json" with { type: "json" };
console.log(config.applicationName);
console.log(config.apiUrl);
The runtime knows that the imported resource must be JSON. In Node.js, omitting the type attribute causes JSON module imports to fail.
TypeScript can also provide useful type information for JSON imports. However, developers should not assume that the JSON file is automatically validated against an application's business rules.
For example, a configuration file may contain a valid JSON structure but still have an invalid API URL or an unsupported environment name.
For important application settings, define an explicit type and validate the configuration when the application starts.
interface AppConfig {
applicationName: string;
environment: "development" | "staging" | "production";
apiUrl: string;
enableLogging: boolean;
}
import rawConfig from "./config.json" with { type: "json" };
const config = rawConfig as AppConfig;
console.log(`Starting ${config.applicationName}`);
The type assertion helps TypeScript understand the expected structure, but it does not perform runtime validation. Use a validation library or custom validation logic when configuration values must be trusted.
Using Import Attributes With Dynamic Imports
Import attributes are also supported with dynamic import expressions.
Dynamic imports are useful when an application needs to load a resource only after a particular condition is met.
For example:
async function loadConfiguration() {
const module = await import("./config.json", {
with: {
type: "json"
}
});
return module.default;
}
const config = await loadConfiguration();
console.log(config.applicationName);
The second argument to import() contains the with object.
This approach can be useful in applications that load configuration based on the selected environment or user action.
For example, a development tool may load different JSON files depending on the selected environment.
async function loadEnvironmentConfig(
environment: string
) {
if (environment === "development") {
return import("./development.json", {
with: { type: "json" }
});
}
return import("./production.json", {
with: { type: "json" }
});
}
Dynamic imports return promises, so the application must handle loading failures. A missing file, invalid JSON resource, or unsupported import attribute can cause the promise to reject.
Using Import Attributes in Node.js
Node.js supports import attributes for module loading. Its current documentation identifies JSON and text as supported module types.
For JSON modules, the type attribute is mandatory.
import settings from "./settings.json" with {
type: "json"
};
console.log(settings);
Node.js also supports text modules through the text type attribute:
import message from "./message.txt" with {
type: "text"
};
console.log(message);
The text attribute tells Node.js to load the resource as text rather than JavaScript.
These examples depend on the Node.js version and runtime support available in the deployment environment. Always test the compiled output using the same Node.js version that will run the application.
There is another important consideration when using TypeScript with Node.js. TypeScript's compiler configuration and Node.js runtime behavior are separate concerns. A successful TypeScript compilation does not guarantee that every imported resource will be supported by the runtime.
Using Import Attributes With Other Module Types
JSON and text are practical examples, but import attributes are designed to support additional resource-loading metadata.
For example, the ECMAScript ecosystem has explored module types such as CSS and WebAssembly. Support depends on the runtime or bundler.
A browser environment may support resource types that are not supported by Node.js. Similarly, a bundler may provide additional handling for assets based on its own configuration.
Developers should not assume that an attribute supported by one environment will work in another.
Before using a nonstandard attribute, check:
- Whether the runtime supports the attribute.
- Whether the bundler understands the attribute.
- Whether the TypeScript compiler accepts the syntax in the selected module mode.
- Whether the production build preserves or transforms the import correctly.
Import attributes provide a standard syntax, but they do not make every module type universally available.
Common Mistakes to Avoid
Using the Old assert Syntax
Older examples may use assert for JSON imports. New code should use with.
// Modern syntax
import data from "./data.json" with { type: "json" };
Review older code during TypeScript upgrades, especially when changing the module configuration.
Assuming TypeScript Handles Runtime Loading
TypeScript understands import attributes, but it does not control how Node.js or a browser loads the resource.
A project can compile successfully and still fail at runtime if the execution environment does not support the requested module type.
Test the actual application runtime rather than relying only on the compiler.
Using an Incorrect Module Configuration
Module configuration affects how TypeScript resolves imports and checks compatibility.
For Node.js applications, use Node-aware settings such as NodeNext or an appropriate version-specific mode. For bundler applications, use a configuration that matches the bundler's module behavior.
Avoid choosing module settings simply because they produce output that looks correct.
Assuming JSON Imports Validate Application Data
The type attribute identifies the resource format. It does not guarantee that the imported data satisfies application requirements.
Validate important configuration values before using them in production.
Mixing Runtime-Specific Attributes
An import attribute supported by a browser or bundler may not be supported by Node.js.
Keep runtime-specific imports isolated where possible, and verify compatibility across development, testing, and production environments.
Best Practices for Production Applications
Use import attributes when the runtime or bundler supports them and the resource type needs to be specified explicitly.
Prefer the modern with syntax in new code. Avoid introducing the older assert syntax into new projects.
Keep TypeScript module settings aligned with the application's execution environment. Node.js applications and bundler-based applications can require different configurations.
Validate external and application-critical JSON data at runtime. A successful import only confirms that the resource was loaded according to the runtime's module rules.
Test both compilation and execution. Include tests that load the actual JSON or text resources used by the application.
Finally, document runtime requirements. If an application depends on a particular Node.js version or bundler feature, make that requirement clear in the project's setup documentation and CI configuration.
Conclusion
TypeScript import attributes provide a standard way to attach resource information to ECMAScript module imports. They are particularly useful when importing JSON and text resources because they allow developers to specify how those resources should be interpreted.
The modern with syntax replaces the older assert syntax and is supported by TypeScript for use with compatible runtimes and tools. Node.js requires the JSON type attribute for JSON module imports and also supports text module imports.
For production applications, the most important considerations are runtime compatibility, correct TypeScript module configuration, and data validation. When these areas are handled properly, import attributes make module loading more explicit and help prevent resource-type errors.
Join the conversation! Your thoughts help the community grow.