14

I'm using ts-node and in tsconfig.json I have "baseUrl": "./src". This way I can import a sibling in src without using ./. However ts-node does not seem to using the baseUrl property in resolving the sibling imports, so I get errors like this:

    > tsmochanyc@1.0.0 test /home/ole/Junk/tsmochanyc
    > mocha -r ts-node/register src/**/*.spec.ts

    Error: Cannot find module 'hello'
        at Function.Module._resolveFilename (internal/modules/cjs/loader.js:548:15)

Curious whether this is a ts-node bug or whether I should be doing something different?

Ole
  • 41,793
  • 59
  • 191
  • 359

1 Answers1

10

As indicated in the issue posted to ts-node tsconfig-paths can be used for this. I have a fully (But minimal) working example here. If you want to try it out just:

git clone https://github.com/oleersoy/tsmochanyc
cd tsmochanyc
npm i
npm test

Look at the the baseUrl and paths propertis in tsconfig.json to see how the resolution works. In this case they have been set so that tsmochanyc proxies src/ this way the path mirrors the same paths that devs would use if tsmochanyc were an external dependency.

Ole
  • 41,793
  • 59
  • 191
  • 359