gpt4 book ai didi

javascript - 如何将 Typescript 编译成使用 ES6 模块的 JS?

转载 作者:行者123 更新时间:2023-12-05 05:37:46 26 4
gpt4 key购买 nike

我的 tsconfig.json 文件包含

{
"compilerOptions": {
"module": "esnext",
"target": "esnext",
}
}

但是编译后的JS文件仍然使用exports/require语法。

我该如何解决这个问题?

编辑:

我的目录结构如下

- APIs/
- emails/
- sendEmail.ts
- oldFiles/
- file1.js
- file2.js
- index.js
- tsconfig.json
- package.json

我从根目录使用命令 tsc APIs/emails/sendEmail.ts 运行 tsc

最佳答案

这些设置是正确的,所以很可能您实际上并没有使用那个 tsconfig.json 文件。例如,如果您执行 tsc someFile.tstsc 不会使用 tsconfig.json(令人惊讶)。你最好的选择可能是添加一个 includes 到你的配置中(这样 tsc 知道它在编译什么)然后通过调用 tsc 简单地编译,这寻找本地tsconfig.json

{
"compilerOptions": {
"module": "esnext",
"target": "esnext"
},
"include": ["**/*.ts"]
}

然后在 package.json 中(例如):

// ...
"scripts": {
"build": "tsc"
}
// ...

关于javascript - 如何将 Typescript 编译成使用 ES6 模块的 JS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73061777/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com