简体   繁体   English

tsc-watch 抛出错误:“文件 'dist/index.js' 是 JavaScript 文件。 您的意思是启用“allowJs”选项吗?”

[英]tsc-watch throwing error: “File 'dist/index.js' is a JavaScript file. Did you mean to enable 'allowJs' option?”

I have a simple index.ts file I complied with tsc:我有一个遵循 tsc 的简单 index.ts 文件:

function printMessage(msg: string): void {
    console.log(`Message: ${msg}`);
}

printMessage("Hello, TypeScript");

When I run the command:当我运行命令时:

npx tsc-watch --onsuccess "node dist/index.js"

I get the error "File 'dist/index.js' is a JavaScript file. Did you mean to enable the 'allowJs option?"我收到错误消息“文件 'dist/index.js' 是 JavaScript 文件。您的意思是启用 'allowJs 选项吗?” I've tried it with the allowJs option to true but it still generates the same error.我已经尝试将 allowJs 选项设置为 true,但它仍然会产生相同的错误。

TypeScript version 3.9.7 tsc-watch version 4.2.9 TypeScript 版本 3.9.7 tsc-watch 版本 4.2.9

I worked around this by installing tsc-watch globally:我通过全局安装 tsc-watch 来解决这个问题:

npm intall -g tsc-watch

Then I could run the command without npx:然后我可以在没有 npx 的情况下运行命令:

tsc-watch --onSuccess "node dist/index.js"

Which worked fine now.现在效果很好。 Looking at your code it seems you are also following the Essential TypeScript book Chapter 5 page 114 in case others will search for these terms.查看您的代码,您似乎也在关注 Essential TypeScript 书第 5 章第 114 页,以防其他人搜索这些术语。

Still don't understand why the original command doesn't work.仍然不明白为什么原始命令不起作用。 As Aluan mentioned in his comment it seems that the index.js file is actually passed to tsc-watch as it complains that it wants to compile a.js file instead of.ts files.正如 Aluan 在他的评论中提到的,似乎 index.js 文件实际上被传递给了 tsc-watch,因为它抱怨它想要编译 a.js 文件而不是 .ts 文件。

This problem is caused by the way that PowerShell parses arguments.这个问题是PowerShell解析arguments的方式造成的。 Here is the escape sequence that will solve the problem:这是解决问题的转义序列:

npx tsc-watch --onSuccess "\`"node dist\index.js\`""

Those are double quotes and back ticks.这些是双引号和反引号。 The result is that PowerShell passes on the "node dist/index.js" argument without parsing it.结果是 PowerShell 传递“node dist/index.js”参数而不解析它。

暂无
暂无

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

相关问题 文件“/Path/hello.js”是一个 JavaScript 文件。 您是要启用“allowJs”选项吗 - File '/Path/hello.js' is a JavaScript file. Did you mean to enable the 'allowJs' option Typescript 使用 tsc-watch 编译 - 文件 dist/server.js 具有不受支持的扩展名 - Typescript compilation using tsc-watch - File dist/server.js has unsupported extension 错误:ENOENT:没有这样的文件或目录,stat \'/app/dist/client/build/index.js\'' - Error: ENOENT: no such file or directory, stat \'/app/dist/client/build/index.js\'' VSCode TypeScript problemMatcher `$tsc-watch` 不看 - VSCode TypeScript problemMatcher `$tsc-watch` not watching tsc-watch 消耗 TSC_NONPOLLING_WATCHER 吗? - Does tsc-watch consume TSC_NONPOLLING_WATCHER? 错误TS5055中的错误:无法写入文件“…”,因为它将使用allowJS和outDir选项覆盖输入文件 - ERROR in error TS5055: Cannot write file '…' because it would overwrite input file with allowJS and outDir option 运行命令 node dist/index.js 给出与 src 文件夹中的 Typescript 文件相关的错误 - Running the command node dist/index.js gives an error related to Typescript files in src folder tsc watch命令抛出错误 - tsc watch command is throwing errors Bazel - 错误:缺少输入文件“//server:files/src/index.js” - Bazel - ERROR: missing input file '//server:files/src/index.js' 带有allowJs的TSC在rootDir外部编译自己的已编译JS - TSC with allowJs compiles its own compiled JS outside rootDir
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM