tsconfig / bases

Hosts TSConfigs to extend in a TypeScript app, tuned to a particular runtime environment
MIT License
6.33k stars 237 forks source link

Make ES2020 default module system? #67

Open Sti2nd opened 3 years ago

Sti2nd commented 3 years ago

Background: I created a new Node project and wanted to use @tsconfig/node14 base in my tsconfig. As EcmaScript modules are fully supported in Node 14 I began using the ESM syntax and got a runtime error because the base specifies CommonJS as module system.

I got the same error as this Stack Overflow post.

```text node server/ file:///C:/Users/stianahj/Documents/wishlist/server/dist/app.js:5 Object.defineProperty(exports, "__esModule", { value: true }); ^ ReferenceError: exports is not defined in ES module scope This file is being treated as an ES module because it has a '.js' file extension and 'C:\Users\stianahj\Documents\wishlist\server\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension. at file:///C:/Users/stianj/Documents/wishlist/server/dist/app.js:5:23 ?[90m at ModuleJob.run (internal/modules/esm/module_job.js:169:25)?[39m ?[90m at async Loader.import (internal/modules/esm/loader.js:177:24)?[39m ?[90m at async Object.loadESM (internal/process/esm_loader.js:68:5)?[39m ```

Discussion point: Since ESM is a stable feature in Node 14, should ESM also be the module system in the @tsconfig/node14? I think it makes sense. Also wrote something in a comment on the docs https://github.com/microsoft/TypeScript-Website/issues/1366#issuecomment-880128638.

Lastly, I can add that overriding the base works by simply specifying the field you want to override in your own tsconfig.json. One can still use the base, so this is just a discussion about what should be the default.

orta commented 3 years ago

We're hoping that Node + ESM support will make it into TypeScript 4.5, https://github.com/microsoft/TypeScript/pull/44501 - so realistically I don't think we should be pushing that as the default here

Sti2nd commented 3 years ago

That weird, I am successfully transpiling TypeScript with ESM and using it in Node 14, and TSC is emitting ESM as well. I thought the only thing needed to match between Node and TSC was the supported EcmaScript (target) version.

So obviously there is something that I am not understanding, and that is ok 🙂 Will wait for TypeScript 4.5 and see what happens.