CommonJS is an older JavaScript module specification that became a de facto standard through Node.js support. It divides large applications into manageable files, but ECMAScript modules are preferred when the runtime supports them. section 4.5
Node.js must know which module format to use. Configure the package.json type property as commonjs, or use the relevant file-extension convention described for the project. section 4.5.1
TypeScript compiled to CommonJS
A TypeScript file can export sum and another file can import it. With an ES5 target, the compiler emits CommonJS-style JavaScript using require and exports; Node.js can execute that output. The generated code includes require("./calc") and invokes the exported sum function, producing "Total: 600". section 5.8
Listing 4.25 Setting the module type in the package.json file in the primer folder
{
"name": "primer",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"nodemon": "^2.0.20"
},
"type": "module"
}
Inspect the package.json structure used to select a module format; changing its type value to commonjs selects CommonJS handling. section 4.5.1


