On this page

Bundlers & Frameworks

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

Why Bundlers Matter​

TypeScript itself does not bundle code.

It only:

Bundlers handle:

Modern TS projects almost always use a bundler—especially in frontend development.


TypeScript with Vite​

Vite is the modern default for frontend TypeScript development.

It’s fast, simple, and uses esbuild for TS transpilation.

Why Vite is great for TypeScript​


1. Create a Vite + TypeScript project​


npm create vite@latest my-app -- --template vanilla-ts

This generates:


vite.config.ts

tsconfig.json

src/main.ts

Everything works out of the box.


2. How Vite handles TypeScript​

Recommended workflow​

package.json


{

  "scripts": {

    "dev": "vite",

    "build": "vite build",

    "typecheck": "tsc --noEmit"

  }

}

This gives you:


3. Vite + React + TypeScript​


npm create vite@latest my-app -- --template react-ts

Everything is preconfigured:

Vite is the easiest way to start a modern TS frontend.


TypeScript with Webpack​

Webpack is older and more configurable than Vite.

It’s still widely used in enterprise environments.

Why Webpack?​

But it requires more configuration.


1. Install Webpack + TS loader​


npm install --save-dev webpack webpack-cli ts-loader typescript

2. Create a Webpack config​

webpack.config.js​


module.exports = {

  entry: "./src/index.ts",

  module: {

    rules: [

      {

        test: /\.ts$/,

        use: "ts-loader",

        exclude: /node_modules/,

      },

    ],

  },

  resolve: {

    extensions: [".ts", ".js"],

  },

  output: {

    filename: "bundle.js",

  },

};

3. Configure TypeScript​

tsconfig.json​


{

  "compilerOptions": {

    "target": "ES2017",

    "module": "ESNext",

    "strict": true

  }

}

4. Build​


npx webpack

Webpack will:

Pros​

Cons​


TypeScript with Node ESM​

Node.js now supports ES modules natively.

This changes how TypeScript must be configured.


1. Enable ESM in Node​

In package.json:


{

  "type": "module"

}

Now Node expects:


2. Configure TypeScript for ESM​

tsconfig.json


{

  "compilerOptions": {

    "module": "ESNext",

    "moduleResolution": "NodeNext",

    "target": "ES2020",

    "outDir": "dist"

  }

}

NodeNext is crucial—it makes TS behave like Node’s ESM loader.


3. Write ESM‑compatible imports​


import { add } from "./utils.js";

Even in TypeScript, you must include .js because the compiled output will be .js.


4. Run with Node​


node dist/index.js

Or use a runtime tool like tsx:


npx tsx src/index.ts

Common Pitfalls with Node ESM​

1. Missing file extensions​


import { foo } from "./foo"; // ❌ fails in Node ESM

Must be:


import { foo } from "./foo.js";

2. Mixing CommonJS and ESM​

Avoid:


import fs from "fs"; // ❌ if using require() elsewhere

3. Incorrect moduleResolution​

Use:


"moduleResolution": "NodeNext"

Not:


"moduleResolution": "Node"

Choosing the Right Tool​

Use CaseRecommended ToolModern frontend appsViteReact, Vue, SvelteViteEnterprise legacy appsWebpackComplex custom pipelinesWebpackNode backend (ESM)tsx or Node ESMNode backend (CommonJS)ts-node or tsx


Summary​

In this lesson, you learned how TypeScript integrates with modern build tools:

1. TypeScript with Vite​

2. TypeScript with Webpack​

3. TypeScript with Node ESM​

Understanding these tools is essential for building real‑world TypeScript applications—frontend or backend.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

ts-node, tsx and Runtime Tools

Next

Linting & Formatting