📜package.json

JSmoduleCommonJS ⟩ package.json

(in Node.js)

  • ES modules can import ES or CommonJS module exports.

  • CommonJS modules can only require CommonJS module exports.

"type": "module"      // ES module
"type": "commonjs"    // CommonJS module (⭐️ default)

                    ES export    CommonJS exports
-------------------------------------------------
ES import           ✅           ✅
CommonJS require    ❌           ✅ 
-------------------------------------------------
⭐️ looks like CommonJS "module.exports" is a better choice❓ 

in an ES module (.mjs / .js of "type":"module"):

  • can only use import / export .

  • can import CommonJS exports.

  • cannot use require.

in an CommonJS module (.cjs / .js of "type":"commonjs"):

  • cannot use require to load an ES module.

Last updated