簡體   English   中英

如何將 Typescript 編譯為使用 ES6 模塊的 JS?

[英]How to compile Typescript to JS that uses ES6 modules?

我的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.ts ,則tsc不使用tsconfig.json (令人驚訝)。 您最好的選擇可能是在您的配置中添加一個includes (以便tsc知道它正在編譯什么),然后通過調用tsc進行編譯,這查找本地tsconfig.json

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

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

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

這個配置應該可以工作。 但它是tsconfig.json而不是tsconfig.js

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM