Rewrite .ts import paths on emit
Write relative .ts imports for source execution and emit runnable .js imports with TypeScript's rewriteRelativeImportExtensions option.
You run src/main.ts with Node's type stripping during development, then use tsc to publish JavaScript. Use rewriteRelativeImportExtensions to keep the .ts path for development and write a .js path into the published file.
Point source and output at existing files
In a scratch project with TypeScript installed, put "type": "module" in package.json. Add this tsconfig.json. The rewrite flag has been available since TypeScript 5.7.
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"rootDir": "src",
"outDir": "dist",
"strict": true,
"rewriteRelativeImportExtensions": true
},
"include": ["src"]
}With this config, tsc will write the JavaScript tree under dist and rewrite relative TypeScript extensions in the emitted imports.
Save this as src/venue.ts.
export const label: string = 'Hall A'The module provides a named export whose type annotation can be stripped.
Save this as src/main.ts, using the extension of the file on disk.
import { label } from './venue.ts'
console.log(label)Run node src/main.ts on a Node release with type stripping enabled, and it prints Hall A.
Run pnpm exec tsc and open dist/main.js. Its import points to ./venue.js, beside it in dist. Run node dist/main.js to check that the emitted entry prints Hall A as well. This catches a broken emitted specifier even if development runs the source without an error.
Look for imports outside the rewrite
The flag looks for relative paths that start with ./ or ../. A TypeScript paths alias such as #venue.ts can resolve during checking and stay spelled that way in the output. Node still needs a way to resolve that alias when it runs your JavaScript. A relative import is enough if both files ship in the same tree.
Exercise computed dynamic imports with the specifiers your app receives at runtime. Inspecting the emitted static imports covers only the literal paths in those statements. For a package, run the JavaScript entry from a folder that contains the published files and runtime dependencies. If that run looks for a .ts file, you have a concrete specifier to trace.