How to import files in nodejs in order to fit tsc build & ts-node-dev?

258 Views Asked by At

When I am working with ts-node-dev, imported files must be named like "./api" or "./api.ts" and when the project is built (using tsc), I need "./api.js" (which is normal... it becomes pure js).

Is there a way to make ts understand that .js imports mean .ts in dev and avoid manual rename after build ?

Error type when named "api.js" :

[INFO] 20:24:15 ts-node-dev ver. 1.1.8 (using ts-node ver. 9.1.1, typescript ver. 4.4.3)
Error: Cannot find module './api/api.js'

And the counterpart if named "api" :

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\xxx\dist\api\api' imported from C:\xxx\dist\main.js

Thanks, C

1

There are 1 best solutions below

0
On

Found solution here : Node can't find modules without .js extension

Simply run :

node --experimental-specifier-resolution=node ./dist/main.js