thanks for your comment,
absolutely, for this setup it would be a little bit tricky, because:
let's put the production scenario above aside, and focus on "dev" only, we can do:
let's clone the repo:
git clone https://github.com/oieduardorabelo/node-typescript
and install tsconfig-paths
npm install tsconfig-paths
inside our tsconfig.json we can define baseUrl and paths:
now we can use the alias #domains in any file, let's update server.ts:
import bookRoutes from '#domains/book/routes';
import reviewRoutes from '#domains/review/routes';
const server = express();
now, let's register the module alias in our nodemon.json:
"ts": "node --require ts-node/register --require tsconfig-paths/register"
now we can start the server with aliases:
npm run dev
and it should work as before, but with aliases!
now, for the Jest mappings, you need to keep the same mappings from tsconfig.json but in the Jest syntax/config, we need to use moduleNameMapper. I'll add the Jest config in package.json for this example:
and now you can run your tests as before:
npm run test
so far, so good.
now, for the production scenario, I do not have a good straight answer, because it requires a considerable amount of work to adapt it per project.
but thinking about the process to make it work, we could use something like module-alias
and we would need:
npm run build
cd dist/ && npm install
node --require module-alias/register entry.js
phew, what a setup! ahahah
i hope it helped! 👋
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.