1

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?

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

6 Comments

My question is set them both as such, "module": "commonjs" & "moduleResolution": "node" seems no need.
Both are the defaults, so neither one is necessary. If you want to output a CJS module, set module to commonjs, and then you don't need to explicitly set the value of moduleResolution.
Hi check my update
Did this answer your question?
I have to say the question I quoted answered my question but your answer didn't (at least not from my point of view). So I just upvoted your answer instead of accepting it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.