如何同时使用普通进口和顶级等待?
我想使用ts-node同时使用导入(<代码>从y 导入x)和顶级等待。但是,如果我将tsconfig.com.pileroptions.module
更改为es2017
或更高的top级>或更高的台面,我会得到:
SyntaxError: Cannot use import statement outside a module
修复此问题是根据无数的GH问题等等设置tsconfig.compileroptions.module
to commonjs
的问题又导致:
Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher
我如何才能两者?必须有一种方法...
tsconfig.json:
{
"compilerOptions": {
"declaration": true,
"module": "esnext",
"target": "es2017",
"moduleResolution": "node",
"esModuleInterop": true,
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"outDir": "dist",
"skipLibCheck": true,
"resolveJsonModule": true
},
"include": ["src/**/*.ts"]
}
package.json:
{
"name": "x",
"version": "0.0.1",
"main": "main.js",
"type": "module",
...
}
我正在使用节点lts(v16.14.2)和typescript 4.6.3。
I want to use imports (import x from y
) and top-level awaits at the same time with ts-node. However if I change my tsconfig.compilerOptions.module
to es2017
or higher as required by top-level awaits I get:
SyntaxError: Cannot use import statement outside a module
The fix for this is according to countless GH issues and SO questions to set tsconfig.compilerOptions.module
to commonjs
which in turn results in:
Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher
How can I have both? There has to be a way...
tsconfig.json:
{
"compilerOptions": {
"declaration": true,
"module": "esnext",
"target": "es2017",
"moduleResolution": "node",
"esModuleInterop": true,
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"outDir": "dist",
"skipLibCheck": true,
"resolveJsonModule": true
},
"include": ["src/**/*.ts"]
}
package.json:
{
"name": "x",
"version": "0.0.1",
"main": "main.js",
"type": "module",
...
}
I am using Node LTS (v16.14.2) and TypeScript 4.6.3.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信这主要对您来说是一个问题,因为您在
tsconfig.json
中使用了“ moduleresolution:“ node”
。更好地设置“模块”:“ NodeNext”
和“ moduleresolution:“ nodeNext”
。此答案与
ts-node
无关,但仅涉及node.js和typescript。 您最好使用tsx
不需要配置而不是ts-node
。给定以下项目:
package.json
tsconfig.json
src/y.ts
src/main.ts
运行
./ node_modules/.bin/tsc
以生成dist/
中的构建。现在运行
node dist/main.js
:使用了node.js和tyscript的以下版本:
如果您需要使用
ts-node < /代码>我可以进一步挖掘,但是您并没有真正解释为什么需要该工具。另外,如果您出于任何原因要直接执行
.ts
文件,我将通过ts-node
考虑tsx
。使用
[email&nbsp; procected] 您可以得到通过运行相同的输出:
I believe this was primarily an issue for you because you used
"moduleResolution: "node"
in yourtsconfig.json
. Nowadays you're much better off setting"module": "nodenext"
and"moduleResolution: "nodenext"
.This answer has nothing to do with
ts-node
but only involves Node.js and TypeScript. You're better off usingtsx
which requires no configuration instead ofts-node
.Given the following project:
package.json
tsconfig.json
src/y.ts
src/main.ts
Now run
./node_modules/.bin/tsc
to generate the build indist/
.Now run
node dist/main.js
:The following versions of Node.js and TypeScript were used:
If you need to use
ts-node
I can dig in further, but you didn't really explain why you needed that tool. Also, I would considertsx
overts-node
if you want to execute.ts
files directly for whatever reason.Using
[email protected]
you can get the same output by running: