dividab / tsconfig-paths

Load node modules according to tsconfig paths, in run-time or via API.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to use bootstrap to run from dist output folder => workaround

0x80 opened this issue · comments

I am compiling my code to dist with the following paths configuration in tsconfig.json:

    "baseUrl": ".",
    "paths": {
      "~/*": ["src/*"]
    }

I tried to use the bootstrapping instructions but I couldn't get it to resolve paths from my dist directory. I think I've tried about every combination of paths and baseUrl imaginable, so I think it's not working (as intended).

Luckily I have found a workaround. I created a file tsconfig.run.json next to my original tsconfig.json with the following contents:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "baseUrl": "dist"
  }
}

And now I can run the code from the dist output like this:

TS_NODE_PROJECT=./tsconfig.run.json node -r tsconfig-paths/register dist/main.js

The instructions in the readme about configuring the tsconfig with -P or --project seem confusing/misleading because this is an option for ts-node. It's not something that node supports.

And I guess the variable TS_NODE_PROJECT is better renamed to something that is not ts-node specific since I'm using it here outside of that context. Maybe something like TS_CONFIG_FILE would be better.

Nevermind! I got it to work.

I was stupid enough to copy the clean() call from the example, which voids the code above it 😄 🤦‍♂️

My bootstrap file is now:

const tsConfigPaths = require("tsconfig-paths");
const baseUrl = "dist";
tsConfigPaths.register({
  baseUrl,
  paths: {
    "~/*": ["./*"],
  },
});