简体   繁体   English

如何在 nodejs 中导入文件以适应 tsc build & ts-node-dev?

[英]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.

相关问题 ts-node-dev 不打印 ts 错误 - ts-node-dev not printing ts errors Npm i ts-node-dev --save-dev 导致没有这样的文件或目录,chmod node_modules/ts-node-dev/lib\bin.js' - Npm i ts-node-dev --save-dev leads to no such file or directory, chmod node_modules/ts-node-dev/lib\bin.js' 运行 ts-node-dev 时,ts-node 的“main”条目无效 - Invalid “main” entry for ts-node when running ts-node-dev 使用 ts-node-dev 运行一个简单的 express 应用程序并得到错误:错误表达式:传递给 `ts.resolveTypeReferenceDirective` 的非字符串值 - Running a simple express app with ts-node-dev and get error: False expression: Non-string value passed to `ts.resolveTypeReferenceDirective` 使用 tsc 将 .json 文件转换为 .ts 文件 - Converting .json files to .ts files using tsc tsc 和 ts-node 忽略 noImplicitAny - tsc and ts-node ignoring noImplicitAny tsc错误地将.ts文件编译为.js - tsc incorrectly compiling .ts files into .js 获取 Node 项目上的 `tsc` 导入依赖 - Get `tsc` import dependencies on Node project 如何在 NodeJS 中导入和导出文件? - How to import and export files in NodeJS? 如何通过es6导入导入nodejs模块“module”以使用“createRequire”创建“require”函数以使用节点的“require”? - How to import nodejs module "module" via es6 import to create "require" function with "createRequire" in order to use node's "require"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM