Sub topic 9
Fix Imports, Exports, and Module Interop
Assume you know JavaScript functions, objects, and either ES modules or CommonJS. You also have a TypeScript project with tsconfig.json, and JavaScript and TypeScript files may still coexist.
Identify the module style
A module is a file that shares values through export and receives values through import. JavaScript has two common module systems:
- ES modules (ESM) use
importandexport. - CommonJS uses
require()andmodule.exports.
Choose one style for new code when possible. Mixing styles during migration is common, but it requires clear compiler settings.
| Style | Export example | Import example |
|---|---|---|
| ES module | `export function add() {}` | `import { add } from "./math";` |
| CommonJS | `module.exports = { add };` | `const { add } = require("./math");` |
Use named and default exports consistently
Named exports describe specific values. The importing code must use the same exported name.
// math.ts
export function add(left: number, right: number): number {
return left + right;
}
export const version = "1.0";// app.ts
import { add, version } from "./math";
console.log(add(2, 3));
console.log(version);A default export represents the main value from a file. The importer can choose its local name.
// formatter.ts
export default function formatName(name: string): string {
return name.trim().toUpperCase();
}// app.ts
import formatName from "./formatter";
console.log(formatName("Ada"));Avoid switching between default and named imports for the same module without a reason. That change often creates confusing errors after a file is renamed.
Configure CommonJS interop
Many older Node.js projects use CommonJS packages. A practical migration configuration is:
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"rootDir": "src",
"outDir": "dist",
"allowJs": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": false
},
"include": ["src"]
}With "module": "commonjs", TypeScript converts ES-style imports and exports into CommonJS output. "esModuleInterop": true adds compatibility helpers for CommonJS packages that do not have a true default export. It also enables synthetic default imports.
For example:
import express from "express";
const app = express();
app.get("/", (_request, response) => {
response.send("OK");
});
app.listen(3000);Without interop support, the compiler may require a namespace import instead:
import * as express from "express";Use the import form that matches the package's type declarations and your runtime configuration. Do not solve every module error by adding a type assertion.
Fix migration errors systematically
When an import fails, check these points:
- Confirm the path and file name, including capitalization.
- Check whether the source uses a named or default export.
- Check whether the package documents CommonJS or ESM usage.
- Inspect the generated JavaScript in
dist. - Avoid adding
.jsto relative imports unless your chosen module mode requires it.
A common error says that a property does not exist on a namespace or default import. Compare the export shape with the import shape instead of disabling checking.
Exercise
A file contains export const port = 3000;. Which import correctly receives that value?
Show answer
Use import { port } from "./config";. Braces indicate a named import. A default import would require export default.