简体   繁体   中英

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

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

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

Simply run:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM