Sub topic 14
TypeScript and Bundlers: Types and Build Output
This chapter assumes you know ES module syntax, have used a tsconfig.json, and have run at least one bundler such as Vite, webpack, or esbuild. It also assumes you have read the earlier chapters on the with { type: "json" } clause and the default-export-only rule.
When you run your code natively, the runtime enforces the rules for JSON modules. When a compiler or bundler sits in between, it applies its own rules. Your code has to satisfy both.
Getting TypeScript to Accept JSON Imports
TypeScript refuses to import .json files unless you turn on resolveJsonModule. TypeScript 5.3 and later also understand the with clause. For a Node.js project that runs native ES modules, use the nodenext module setting:
{
"compilerOptions": {
"target": "es2022",
"module": "nodenext",
"moduleResolution": "nodenext",
"resolveJsonModule": true,
"strict": true,
"outDir": "dist"
}
}With module: "nodenext", TypeScript (from version 5.7) checks JSON imports in ES module files the same way Node.js does. A JSON import without with { type: "json" } is an error, and so is a named import. That means type errors catch the mistakes that would otherwise only show up at runtime.
What Type You Get
TypeScript works out the type from the file's contents. With the config.json used throughout this tutorial, the import has a matching structural type:
import config from "./config.json" with { type: "json" };
// Inferred: { appName: string; version: number; features: string[] }
const name: string = config.appName;
const count: number = config.features.length;
console.log(name, count);The types are widened. appName is string, not "Inventory", and features is string[], not a tuple. If you need a stricter contract, check the data against an interface you declare yourself:
import raw from "./config.json" with { type: "json" };
interface AppConfig {
appName: string;
version: number;
features: ("search" | "export")[];
}
const config = raw satisfies Omit<AppConfig, "features"> as AppConfig;
console.log(config.features);What Bundlers Do with JSON Imports
Bundlers usually inline JSON. They parse the file at build time and emit it as a plain JavaScript object, and the import attribute does not appear in the output.
This has consequences:
| Behavior | Native runtime | Typical bundler |
|---|---|---|
| `with { type: "json" }` required | Yes | Often optional |
| Named imports such as `import { appName }` | Link error | Often allowed (webpack, Vite) |
| MIME type checked | Yes, in browsers | No, data is inlined |
| Separate network request | Yes | No |
| One shared object | Yes | Yes, within one bundle |
The named-import row is the main trap. Code that builds cleanly with webpack or Vite can fail once you run it unbundled, for example in Node.js tests or a <script type="module"> prototype. If you write the with clause and import only the default export, the same code works in both places.
Older bundler versions may reject with and accept only assert, or neither. If your build fails on the with keyword, upgrade the bundler before rewriting code to the legacy syntax.
For bundled projects, set "module": "preserve" (or "esnext") with "moduleResolution": "bundler" and keep resolveJsonModule on.
Exercise
A project builds with Vite and uses import { version } from "./config.json";. The team adds Vitest-free unit tests that run the same file directly in Node.js. What fails, and what is the portable fix?
Show answer
Node.js loads config.json as a native JSON module. It rejects the import because the with { type: "json" } attribute is missing, and named imports are not allowed. The portable fix is import config from "./config.json" with { type: "json" }; const { version } = config;. Setting TypeScript to module: "nodenext" would also report both problems at compile time.
Exercise
Why does a browser never perform a MIME type check for a JSON file that Vite bundled?
Show answer
Vite parses the JSON at build time and inlines it into the JavaScript bundle as an object literal. The browser never requests the .json file, so it has no response whose Content-Type could be checked.