DEV Community

Libme
Libme

Posted on

TypeScript Path Aliases Work in the Editor but Fail at Runtime: tsc-alias vs Subpath Imports vs a Bundler

TypeScript's paths option only teaches the type checker where to look; tsc does not rewrite the import specifiers in the JavaScript it emits. So import { db } from "@/lib/db" compiles cleanly, then Node throws Cannot find module '@/lib/db'. You can fix it by rewriting aliases after compile (tsc-alias), by switching to Node's native subpath imports (#lib/*), by bundling, or by running through a tool that resolves paths itself (tsx).

Why does the editor say everything is fine while Node crashes?

Here is the setup that trips people up:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": { "@/*": ["src/*"] },
    "outDir": "dist",
    "module": "commonjs"
  }
}
Enter fullscreen mode Exit fullscreen mode
// src/server.ts
import { db } from "@/lib/db";
Enter fullscreen mode Exit fullscreen mode

tsc succeeds, autocomplete works, and then:

$ node dist/server.js
Error: Cannot find module '@/lib/db'
Require stack:
- /app/dist/server.js
Enter fullscreen mode Exit fullscreen mode

(With ESM output the message reads ERR_MODULE_NOT_FOUND / Cannot find package '@', because Node treats @/lib/db as a scoped package name.) If you open dist/server.js, the specifier is still @/lib/db. The TypeScript team has long treated this as intentional: paths describes how some other tool will resolve modules at runtime, and the compiler does not transform import text. The checker and the runtime disagree, and nothing warns you.

The dead ends I see most often: adding baseUrl (changes nothing at runtime), copying tsconfig.json into the Docker image (Node never reads it), and setting NODE_PATH (works for bare names, not for @/ prefixes in ESM, and it hides the real problem).

Takeaway: if the output JavaScript still contains your alias, the compiler did its job and the runtime has no idea what the alias means.

Which fix should you use?

Approach How it works Best for Real drawback
tsc-alias after tsc Rewrites alias specifiers in dist/ to relative paths Libraries and servers you already build with plain tsc An extra build step; ESM output also needs correct file extensions, which it may not add for you
Node subpath imports (# prefix) package.json imports field, resolved natively by Node New projects that want zero extra tooling Aliases must start with #, so you cannot keep @/
Bundler (tsup, esbuild) Resolves paths while bundling Apps deployed as a single artifact You are now maintaining a bundler config and source maps
tsx (dev/scripts only) Honors tsconfig paths at run time Local dev, scripts, one-off tooling Not a production answer if you ship dist/

How do I fix it with tsc-alias?

This is the smallest change to an existing project.

npm install --save-dev tsc-alias
Enter fullscreen mode Exit fullscreen mode
{
  "scripts": {
    "build": "tsc && tsc-alias"
  }
}
Enter fullscreen mode Exit fullscreen mode

After npm run build, open one file in dist/ and confirm the import became something like require("./lib/db"). That check takes ten seconds and is the only proof that matters. If you use ESM output, also check whether the rewritten specifiers carry file extensions; Node's ESM loader requires them, and that is a separate failure from the alias problem.

If you want the packaged version of this approach without writing any config, tsup is the tool that bundles TypeScript and resolves your paths in one step (see below).

Takeaway: tsc && tsc-alias fixes the alias at the one point where it actually breaks, the emitted files, and you can verify it by reading dist/.

Are Node subpath imports better than path aliases?

They remove the whole class of problem, because the runtime and the type checker read the same declaration. In package.json:

{
  "name": "my-service",
  "type": "module",
  "imports": {
    "#lib/*": "./dist/lib/*.js"
  }
}
Enter fullscreen mode Exit fullscreen mode
import { db } from "#lib/db.js";
Enter fullscreen mode Exit fullscreen mode

Rules worth knowing: every key must begin with #, and Node resolves them relative to the nearest package.json. TypeScript understands the imports field when moduleResolution is node16, nodenext, or bundler. In my setup, with outDir and rootDir set, the checker mapped the dist target back to the source file, but verify this with tsc --noEmit rather than trusting me, because it depends on your layout. As of late 2026 this is supported by current Node LTS releases and by the major test runners.

The drawback is cosmetic but real: you give up @/, and a large codebase with thousands of existing @/ imports needs a codemod to migrate.

Takeaway: for a new project, subpath imports are the only option where nothing can drift out of sync, because there is no second resolver to configure.

What about bundlers and tsx?

If you already ship a bundled artifact, let the bundler resolve the alias:

// tsup.config.ts
import { defineConfig } from "tsup";

export default defineConfig({
  entry: ["src/server.ts"],
  format: ["esm"],
  tsconfig: "tsconfig.json", // paths are read from here
  clean: true,
});
Enter fullscreen mode Exit fullscreen mode

esbuild-based tools read paths from the tsconfig, so the alias disappears at bundle time. The cost is that stack traces point at bundled output unless you enable source maps, and native or dynamically required dependencies sometimes need to be marked external.

For development, tsx src/server.ts runs TypeScript directly and respects paths, which is why aliases "work" locally and then break in the container: dev uses tsx, production runs node dist/. That mismatch is the most common root cause I run into. Older ts-node setups need tsconfig-paths/register, and if you hit that, switching to tsx is usually less work than tuning the register hook.

Test runners have their own resolver as well. Jest needs moduleNameMapper, and Vitest needs resolve.alias or a tsconfig-paths plugin. A green test suite therefore says nothing about whether node dist/ will start.

Takeaway: every tool in your pipeline resolves modules independently, so test the exact command production runs, not the one your editor and test runner use.

How do I stop this from reaching production?

Add a smoke step to CI that runs the built output, not the source:

npm run build
node -e "import('./dist/server.js')" || exit 1
Enter fullscreen mode Exit fullscreen mode

Better still, give the app a --check flag or an import-only entry that loads every module and exits. A failed import in CI costs a minute. The same failure after deploy costs a rollback.

FAQ

Why does TypeScript not rewrite path aliases when compiling?
paths is a type-checking hint, and the compiler deliberately leaves import specifiers unchanged in the output. Something else (a bundler, a post-processor, or Node itself) must resolve them at runtime.

Does Node read tsconfig.json paths?
No. Node never reads tsconfig.json. Use the package.json imports field with # aliases, or rewrite the specifiers at build time.

Why do my path aliases work with tsx but not with node?
tsx resolves tsconfig paths itself, while plain node does not. Run node dist/ locally once to reproduce what production sees.

Bottom line

If you have an existing tsc build and @/ imports everywhere, add tsc-alias and check dist/. If you are starting a service today, use # subpath imports and skip the problem. If you already bundle for deployment, tsup handles the alias as part of work it is doing anyway. Whichever you choose, run the built output in CI so a resolver mismatch fails a pipeline instead of a deploy.

Related reading

Top comments (1)

Collapse
 
irck_ysia_d79705b3a189af9 profile image
Irck Ysia •

Solid #node writeup on "TypeScript Path Aliases Work in the Editor but Fail at Runtime: tsc-al"! Native Node 22+ APIs make zero-dependency services so clean—I've been building a raw-fetch Telegram bot + static site generator all weekend myself. Dropping a heart!