我想使用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