1

What module to use in tsconfig, commonjs or es6?

How to make desicion? I need that output module will work in client/back sides.

POV
  • 11,293
  • 34
  • 107
  • 201

1 Answers1

2

So here we are talking about the module option that will be used by typescript to determine what is the name of the module that will compile your code to the targeted version of javascript you specified with the option target.

So the underlying question you are asking is, what is my target ? Should I target ES3, ES5, ES6, ES7, ES8 or ... ES42 ?


Answer : the compatibility.

In 2020 you propably target ES5 or ES6 (which is the default value).

(You can ignore CommonJS because it relate to ES3 which is 99% chance irrelevant to you)

some article


If your code is made to be executed on browsers, I would recommand you to look which is the latest version supported by all your targeted browser and take the one that is supported by all.

Ex: Safari ES6, Firefox ES8, Chrome ES8 : so you choose ES6 as target so your code works on every targeted browser.

The website caniuse.com is usefull to know which features are supported and which are not


If your code is made to run on backend (node.js), look at which version of node.js is running. Every version of node have different capabilities.

You can have a look here

enter image description here



Additionnal materials :

What version of Javascript is supported in node.js

Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
  • So, does it mean to use ES6 for modules (building npm package)? – POV Mar 24 '20 at 15:14
  • @OPV I would personnaly target ES6. If you encounter any BUG in your tests, downgrade to ES5 but it's unlikely, nowaday every browser and node supports most of ES features. [article](https://itnext.io/why-you-should-use-es6-56bd12f7ae09) – Orelsanpls Mar 24 '20 at 15:19
  • @Orelsanpls What if my code runs in both browser and nodejs? What should I choose in that case? – darKnight Jun 08 '23 at 11:54