absolutely, for this setup it would be a little bit tricky, because:
we are not using a bundler (e.g. webpack, rollup etc)
if you use a module alias, the output Node.js code (e.g. inside "dist/*.js"), will have the aliased path, since TypeScript does not replace the import path string
because of that, you will need to maintain some kind of module alias resolution in the prod version for Node.js only (e.g. you need to start your server with a module alias solution)
we also need to keep mappings for Jest, because Jest doesn't use TypeScript module resolution
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
cd node-typescript
npm install
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:
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:
and install tsconfig-paths
inside our
tsconfig.jsonwe can definebaseUrlandpaths:now we can use the alias
#domainsin any file, let's updateserver.ts:now, let's register the module alias in our
nodemon.json:now we can start the server with aliases:
and it should work as before, but with aliases!
now, for the Jest mappings, you need to keep the same mappings from
tsconfig.jsonbut in the Jest syntax/config, we need to usemoduleNameMapper. I'll add the Jest config inpackage.jsonfor this example:and now you can run your tests as before:
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(e.g. typescript => node.js in dist/)package.jsonwithmodule-aliasinsidedist/tsconfig.jsoninsidedist/package.jsonbut usingmodule-aliassyntaxcd dist/ && npm installto installmodule-aliasfor productionnode --require module-alias/register entry.jsphew, what a setup! ahahah
i hope it helped! 👋