Qiulang
Qiulang

Reputation: 12395

Typescript compilerOptions module & moduleResolution

I find many tsconfig.json samples always have compilerOptions like this

"module": "commonjs",
"moduleResolution": "node"

I feel setting them both as such seems unnecessary because if moduleResolution is node the module is definitely commonjs. The module is commonjs, moduleResolution is definitely node too (I can't think of any other case)

Isn't that is the case?

--- update ---

Now I realize it is not always the case because nodejs has fully support ES Modules, so I can use "module": "ES2020" & "moduleResolution": "node" (for nodejs 14+) but of course if I set "module":"commonjs" I don't need to set "moduleResolution": "node"

Further refer to What TypeScript configuration produces output closest to Node.js 14 capabilities?

Upvotes: 1

Views: 14733

Answers (1)

jsejcksn
jsejcksn

Reputation: 33701

Being explicit about configuration can prevent undesired behavior if a default value changes in the future in a breaking way.

The documentation explains the relationships between these configuration properties. I'll inline the default behaviors below:

moduleResolution:

Default:

Classic if module is AMD, UMD, System or ES6/ES2015,

Matches if module is node12 or nodenext,

Node otherwise.

Allowed:

  • classic
  • node

module:

Default:

CommonJS if target is ES3 or ES5,

ES6/ES2015 otherwise.

Allowed:

  • none
  • commonjs
  • amd
  • umd
  • system
  • es6/es2015
  • es2020
  • es2022
  • esnext
  • node12
  • nodenext

target:

Default:

ES3

Allowed:

  • es3
  • es5
  • es6/es2015
  • es2016
  • es2017
  • es2018
  • es2019
  • es2020
  • es2021
  • esnext

Upvotes: 3

Related Questions