Fix TypeScript path aliases that fail at runtime in Node.js
If tsc succeeds but node dist/main.js throws ERR_MODULE_NOT_FOUND for an alias like @app/format, look at the import in the emitted JavaScript. TypeScript's paths setting tells the compiler where to find a module and leaves the import string intact. For a Node.js app that runs the compiled files directly, use a relative .js import for a nearby file or a # alias defined in package.json for imports across directories. TypeScript paths documentation
This comes up when an AI coding agent copies an alias from a bundled frontend into a server or CLI. The editor can resolve the import and the compiler can build the app. The failure only shows up when plain Node tries to load the output.
Reproduce ERR_MODULE_NOT_FOUND after a successful build
Use a scratch directory and a recent Node.js release. The example is a small ES module project with no bundler. Create this package.json.
{
"name": "alias-demo",
"private": true,
"type": "module",
"scripts": {
"build": "tsc",
"smoke": "node dist/main.js"
}
}
Create tsconfig.json. The @app/* pattern tells TypeScript to look for the matching .ts file in src.
{
"compilerOptions": {
"module": "NodeNext",
"rootDir": "./src",
"outDir": "./dist",
"strict": true,
"paths": {
"@app/*": ["./src/*.ts"]
}
},
"include": ["src/**/*.ts"]
}
Put this function in src/format.ts.
export function formatTotal(cents: number): string {
return `$${(cents / 100).toFixed(2)}`;
}
Then create src/main.ts.
import { formatTotal } from '@app/format';
console.log(formatTotal(425));
Install TypeScript once, then run the two scripts.
npm install --save-dev typescript
npm run build
npm run smoke
The build succeeds. The smoke check exits with ERR_MODULE_NOT_FOUND and Cannot find package '@app/format'. Open dist/main.js and you'll see the same @app/format import. Node interprets it as a package name.
Fix a nearby import with a relative .js path
Delete the paths entry from the example config and change src/main.ts to use a relative import.
import { formatTotal } from './format.js';
console.log(formatTotal(425));
The .js extension points to the file that will exist at runtime. TypeScript resolves it to src/format.ts during compilation and Node loads dist/format.js after the build. Node's ES module loader requires an extension on relative file imports, so use ./format.js here. Node.js ES module file extensions
npm run build
npm run smoke
The smoke check now prints $4.25.
Use package.json imports for a Node.js alias
For an alias that works from multiple directories in the same package, add an imports field at the top level of package.json, alongside type and scripts. This pattern maps #app/format to ./dist/format.js. Node.js subpath imports and patterns
{
"imports": {
"#app/*": "./dist/*.js"
}
}
Keep paths out of this example's tsconfig.json and leave module, rootDir and outDir as shown. In a local package, NodeNext uses rootDir and outDir to trace an imports target in dist back to its TypeScript source. A clean build can therefore resolve #app/format before dist/format.js exists. TypeScript package.json imports resolution
Update src/main.ts, then rerun npm run build and npm run smoke. This version also prints $4.25.
import { formatTotal } from '#app/format';
console.log(formatTotal(425));
If the build works locally and the deployed app fails with ERR_PACKAGE_IMPORT_NOT_DEFINED, check the package.json that ships beside dist. The deployed package needs the imports map and the matching file layout. Copying only the compiled folder leaves the mapping behind. If the map exists and Node reports ERR_MODULE_NOT_FOUND for a path inside dist, check that the target file was built and copied.
Give your coding agent a runtime check
Run the same command your deployment uses, with the same compiled files and package.json. For a long-running server, start it in the test and make a request to a route that loads the affected module. A source-level test can miss the failure when its test runner resolves aliases on Node's behalf.
Find the failing import in the compiled JavaScript. Show how the production
start command resolves it. Make the smallest change that fixes resolution and
run that start command from a clean build. If deployment copies a subset of
files, test that layout too. Record any command that fails before the fix.
If a bundled Vite frontend is the entry point that fails, check which aliases its build understands. The Vite deployment debugging guide covers how to test the output your host actually serves.