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