Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should ts-node examine the baseUrl tsconfig.json property?

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?

like image 512
Ole Avatar asked Jun 21 '18 00:06

Ole


People also ask

Does TS-node use Tsconfig JSON?

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.

What is Tsconfig JSON in TypeScript?

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.

What is module in Tsconfig JSON?

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.


1 Answers

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.

like image 143
Ole Avatar answered Oct 05 '22 15:10

Ole