如何同时使用普通进口和顶级等待?

发布于 2025-01-27 06:20:03 字数 1183 浏览 3 评论 0原文

我想使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2025-02-03 06:20:03

我相信这主要对您来说是一个问题,因为您在tsconfig.json 中使用了“ moduleresolution:“ node”。更好地设置“模块”:“ NodeNext”“ moduleresolution:“ nodeNext”

此答案与ts-node无关,但仅涉及node.js和typescript。 您最好使用tsx 不需要配置而不是ts-node

给定以下项目:

.
├── src/
│   ├── y.ts
│   └── main.ts
├── package.json
└── tsconfig.json

package.json

"type": "module"

tsconfig.json

{
  "compilerOptions": {
     "target": "ESNext",
     "module": "NodeNext",
     "moduleResolution": "Nodenext",
     "outDir": "dist"
  },
  "include": ["src"]
}

src/y.ts

const doSomethingAsync = () => Promise.resolve('something async')

export { doSomethingAsync }
export default 'y'

src/main.ts

import { doSomethingAsync } from './y.js'
import x from './y.js'

console.log(await doSomethingAsync())
console.log(x)

运行./ node_modules/.bin/tsc以生成dist/中的构建。

现在运行node dist/main.js

something async
y

使用了node.js和tyscript的以下版本:

  • node.js 22.1.0
  • typescript 5.4.5

如果您需要使用ts-node < /代码>我可以进一步挖掘,但是您并没有真正解释为什么需要该工具。另外,如果您出于任何原因要直接执行.ts文件,我将通过ts-node考虑tsx

使用[email&nbsp; procected] 您可以得到通过运行相同的输出:

./node_modules/.bin/tsx src/main.ts

I believe this was primarily an issue for you because you used "moduleResolution: "node" in your tsconfig.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 using tsx which requires no configuration instead of ts-node.

Given the following project:

.
├── src/
│   ├── y.ts
│   └── main.ts
├── package.json
└── tsconfig.json

package.json

"type": "module"

tsconfig.json

{
  "compilerOptions": {
     "target": "ESNext",
     "module": "NodeNext",
     "moduleResolution": "Nodenext",
     "outDir": "dist"
  },
  "include": ["src"]
}

src/y.ts

const doSomethingAsync = () => Promise.resolve('something async')

export { doSomethingAsync }
export default 'y'

src/main.ts

import { doSomethingAsync } from './y.js'
import x from './y.js'

console.log(await doSomethingAsync())
console.log(x)

Now run ./node_modules/.bin/tsc to generate the build in dist/.

Now run node dist/main.js:

something async
y

The following versions of Node.js and TypeScript were used:

  • Node.js 22.1.0
  • TypeScript 5.4.5

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 consider tsx over ts-node if you want to execute .ts files directly for whatever reason.

Using [email protected] you can get the same output by running:

./node_modules/.bin/tsx src/main.ts
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文