[英]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).当我使用 ts-node-dev 时,导入的文件必须命名为“./api”或“./api.ts”,并且在构建项目时(使用 tsc),我需要“./api.js” (这是正常的......它变成了纯js)。
Is there a way to make ts understand that.js imports mean.ts in dev and avoid manual rename after build?有没有办法让 ts 明白 .js 在 dev 中导入 mean.ts 并避免在构建后手动重命名?
Error type when named "api.js":命名为“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":如果名为“api”,则对应:
Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\xxx\dist\api\api' imported from C:\xxx\dist\main.js
Thanks, C谢谢,C
Found solution here: Node can't find modules without.js extension在这里找到解决方案: Node can't find modules without.js extension
Simply run:只需运行:
node --experimental-specifier-resolution=node ./dist/main.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.