javascript - How to compile Typescript to JS that uses ES6 modules? - Stack Overflow

My tsconfig.json file contains{"pilerOptions": {"module": "esnext","

My tsconfig.json file contains

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

But the piled JS files still use exports/require syntax.

How do I fix this?

EDIT:

My directory structure is as follows

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

I am running tsc using the mand tsc APIs/emails/sendEmail.ts from the root directory.

My tsconfig.json file contains

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

But the piled JS files still use exports/require syntax.

How do I fix this?

EDIT:

My directory structure is as follows

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

I am running tsc using the mand tsc APIs/emails/sendEmail.ts from the root directory.

Share Improve this question edited Jul 21, 2022 at 7:59 Will Zap asked Jul 21, 2022 at 6:45 Will ZapWill Zap 932 silver badges10 bronze badges 3
  • How are you calling tsc? – T.J. Crowder Commented Jul 21, 2022 at 7:10
  • from the root folder, i call tsc a/b/c/file.ts (the tsconfig.json is in the root folder) – Will Zap Commented Jul 21, 2022 at 7:54
  • Your edit confirms my answer. By calling tsc that way, you're not using your tsconfig.json. (This is surprising, but true.). Just use tsc with appropriate includes (see the answer), rather than giving a specific file. – T.J. Crowder Commented Jul 21, 2022 at 8:05
Add a ment  | 

2 Answers 2

Reset to default 5

Those settings are correct, so most likely you're not actually using that tsconfig.json file. For instance, if you do tsc someFile.ts, tsc doesn't use the tsconfig.json (surprisingly). Your best bet is probably to add an includes to your configuration (so tsc knows what it's piling) and then pile simply by invoking tsc, which will look for a local tsconfig.json.

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

Then in package.json (for instance):

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

This config should work. But it's tsconfig.json not tsconfig.js.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745099637a4611194.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信