paths from tsconfig.json doesn't work after tsc paths from tsconfig.json doesn't work after tsc typescript typescript

paths from tsconfig.json doesn't work after tsc


for anyone still stuck on this:

node -r ts-node/register/transpile-only -r tsconfig-paths/register dist/index.js

this is what you need to do.


This was asked around in many places, and apparently Typescript can make path aliases for development, but not production (Don't quote me on that, have been working with it for barely a month).

To work around that, I installed 'module-alias', a package that solves the path issue after build, without interfering with development.

I was building an Express-js app, and had these files:

server.js:

import env from '@env';import app from './app';app.listen(env.SERVER_PORT || 3000);

tsconfig.json (relevant parts):

{  "compilerOptions": {    "baseUrl": "./src",    "paths": {      "@env": ["path/to/env"]    }  }}

This resolved the path during development runtime, but not after building with 'tsc'. To work around it, I added the 'module-alias' package and did the following changes:

server.js:

import './paths';import env from '@env';import app from './app';app.listen(env.SERVER_PORT || 3000);

paths.js

import 'module-alias/register';import { addAliases } from 'module-alias';addAliases({  '@env': `${__dirname}/path/to/env`,});

This ensured that @env would resolve in both dev runtime and after building.Hope it helps!