简体   繁体   English

为什么 ts-node 不编译打字稿文件?

[英]Why is ts-node not compiling typescript file?

Why is ts-node not compiling and executing typescript file?为什么 ts-node 不编译和执行打字稿文件? It is used in terminal like "ts-node scriptfile" It just returns a blank line.它在像“ts-node scriptfile”这样的终端中使用它只是返回一个空行。 What is wrong?怎么了? Thanks for the help.谢谢您的帮助。

There is another simple way of running .ts files 还有另一种运行.ts文件的简单方法

First of all you have to install typescript 首先你要安装打字稿

npm i -g typescript

then run your .ts file with this command 然后使用此命令运行.ts文件

tsc fileName.ts | node fileName.js

Note: the command above will generate an extra file named fileName.js 注意:上面的命令将生成一个名为fileName.js的额外文件

and finally make sure that you are using console.log() to return anything 最后确保您使用console.log()返回任何内容

ts-node is a typescript executor, not a compiler. ts-node是一个打字稿执行器,而不是一个编译器。 If you run npx ts-node script.ts it will execute as typescript, without compiling.如果您运行npx ts-node script.ts它将作为打字稿执行,而无需编译。

To generate a compiled js file, try tsc script.ts .要生成已编译的 js 文件,请尝试tsc script.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么tsc编译输出一个在运行时抛出异常但ts-node正确运行ts文件的文件? - Why tsc compile output a file that throw exception on running but ts-node run the ts file correctly? Typescript 的声明合并使用 ts-node 无法按预期工作 - Typescript's declaration merging not working as expected using ts-node ts-node 和自定义加载器 - ts-node and custom loaders ts-node 中未显示错误 - Errors not showing in ts-node 尝试将 jest 与 create-react-app 和 typescript 一起使用。 获取错误:开玩笑:无法解析 TypeScript 配置文件...找不到模块“ts-node” - Trying to use jest with create-react-app and typescript. Get error: Jest: Failed to parse the TypeScript config file... Cannot find module 'ts-node' discord.js typescript 错误 - 执行 'ts-node index.ts' 时缺少 index.ts - discord.js typescript error - missing index.ts when executing 'ts-node index.ts' ts-node TypeError [ERR_UNKNOWN_FILE_EXTENSION]:未知文件扩展名“.ts” - ts-node TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" 尝试运行 ts-node 脚本时出现未知文件扩展名“.ts”错误 - Unknown file extension ".ts" error appears when trying to run a ts-node script ava + ts-node 将.spec.ts 文件转换成.ts - ava + ts-node converting .spec.ts files into .ts TS 节点:找不到源文件 - TS-Node: Could not find sourceFile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM