📜package.json
JS ⟩ module ⟩ CommonJS ⟩ package.json
(in Node.js)
ES modules can
importES or CommonJS module exports.CommonJS modules can only
requireCommonJS 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
requireto load an ES module.
.cjs-> CommonJS module..mjs-> ES module..js"module" -> ES module.
"commonjs" -> CommonJS module.
Last updated
Was this helpful?