如何在ts-node中执行一个使用' import type '文件的TypeScript



我想使用ts-node作为一个"模块"来利用import type语法

使用.ts扩展时,我得到警告Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.,然后是错误SyntaxError: Unexpected token 'export'

使用.mts扩展时,我得到错误SyntaxError: Cannot use import statement outside a module.

我需要做什么才能在ts-node中执行单个TypeScript文件并使用import type而不改变package.json中的type?

在我的例子中,我使用vite与react

在我的tsconfig.json文件中,添加以下部分

{
...
"ts-node": {
"compilerOptions": {
"module": "commonjs"
}
},
}

然后直接运行npx ts-node path/to/your/script

相关内容

最新更新