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:
> [email protected] 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?
ts-node automatically finds and loads tsconfig. json . Most ts-node options can be specified in a "ts-node" object using their programmatic, camelCase names. We recommend this because it works even when you cannot pass CLI flags, such as node --require ts-node/register and when using shebangs.
The tsconfig.json file specifies the root files and the compiler options required to compile the project. JavaScript projects can use a jsconfig.json file instead, which acts almost the same but has some JavaScript-related compiler flags enabled by default.
Sets the module system for the program. See the Modules reference page for more information. You very likely want "CommonJS" for node projects. Changing module affects moduleResolution which also has a reference page.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With