Módulos

Tipos

Cenários

/codes/ecma/modules/function-sum/index.js
function sum(a, b) {
  return a + b;
}
 
console.log(sum(2, 1)); //=> 3
 
/codes/ecma/modules/function-sum/index.js
function sum(a, b) {
  return a + b;
}
 
console.log(sum(2, 1)); //=> 3
 
src
├── lib.js
└── main.js
src
├── lib.js
└── main.js
Uma FunçãoVárias Funções
CJS1.12.1
ESM1.22.2

Cenário 1.1 - Uma função no CJS

/codes/ecma/modules/cjs-default/lib.js
function sum(a, b) {
  return a + b;
}
 
module.exports = sum;
 
/codes/ecma/modules/cjs-default/lib.js
function sum(a, b) {
  return a + b;
}
 
module.exports = sum;
 
/codes/ecma/modules/cjs-default/main.js
const sum = require('./lib.js');
 
console.log(sum(2, 1)); //=> 3
 
/codes/ecma/modules/cjs-default/main.js
const sum = require('./lib.js');
 
console.log(sum(2, 1)); //=> 3
 

Cenário 1.2 - Uma função no ESM

/codes/ecma/modules/esm-default/lib.js
function sum(a, b) {
  return a + b;
}
 
export default sum;
 
/codes/ecma/modules/esm-default/lib.js
function sum(a, b) {
  return a + b;
}
 
export default sum;
 
/codes/ecma/modules/esm-default/main.js
import sum from './lib.js';
 
console.log(sum(2, 1)); //=> 3
 
/codes/ecma/modules/esm-default/main.js
import sum from './lib.js';
 
console.log(sum(2, 1)); //=> 3
 

Warning: To load an ES module, set “type”: “module” in the package.json or use the .mjs extension.

/codes/ecma/modules/esm-default/package.json
{
  "name": "module-esm-default",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
 
/codes/ecma/modules/esm-default/package.json
{
  "name": "module-esm-default",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
 

Cenário 2.1 - Várias funções no CJS

/codes/ecma/modules/cjs-named/lib.js
function sum(a, b) {
  return a + b;
}
 
function minus(a, b) {
  return a - b;
}
 
module.exports = { sum, minus };
 
/codes/ecma/modules/cjs-named/lib.js
function sum(a, b) {
  return a + b;
}
 
function minus(a, b) {
  return a - b;
}
 
module.exports = { sum, minus };
 
/codes/ecma/modules/cjs-named/main.js
const { sum, minus } = require('./lib.js');
 
console.log(sum(2, 1)); //=> 3
console.log(minus(2, 1)); //=> 1
 
/codes/ecma/modules/cjs-named/main.js
const { sum, minus } = require('./lib.js');
 
console.log(sum(2, 1)); //=> 3
console.log(minus(2, 1)); //=> 1
 

Cenário 2.2 - Várias funções no ESM

/codes/ecma/modules/esm-named/lib.js
function sum(a, b) {
  return a + b;
}
 
function minus(a, b) {
  return a - b;
}
 
export { sum, minus };
 
/codes/ecma/modules/esm-named/lib.js
function sum(a, b) {
  return a + b;
}
 
function minus(a, b) {
  return a - b;
}
 
export { sum, minus };
 
/codes/ecma/modules/esm-named/main.js
import { sum, minus } from './lib.js';
 
console.log(sum(2, 1)); //=> 3
console.log(minus(2, 1)); //=> 1
 
/codes/ecma/modules/esm-named/main.js
import { sum, minus } from './lib.js';
 
console.log(sum(2, 1)); //=> 3
console.log(minus(2, 1)); //=> 1
 

Resumo dos Cenários

Uma FunçãoVárias Funções
CJS
 
module.exports = sum
const sum = require('./lib')
 
CJS
 
module.exports = sum
const sum = require('./lib')
 
CJS
 
module.exports = { sum, minus }
const { sum, minus } = require('./lib')
 
CJS
 
module.exports = { sum, minus }
const { sum, minus } = require('./lib')
 
ESM
 
export default sum
import sum from './lib'
 
ESM
 
export default sum
import sum from './lib'
 
ESM
 
export { sum, minus }
import { sum, minus } from './lib'
 
ESM
 
export { sum, minus }
import { sum, minus } from './lib'
 

Named e Default

/codes/ecma/modules/esm-default-named/lib.js
export const PI = 3.14;
 
export function sum(a, b) {
  return a + b;
}
 
export function minus(a, b) {
  return a - b;
}
 
export default { sum, minus, PI };
 
/codes/ecma/modules/esm-default-named/lib.js
export const PI = 3.14;
 
export function sum(a, b) {
  return a + b;
}
 
export function minus(a, b) {
  return a - b;
}
 
export default { sum, minus, PI };
 
/codes/ecma/modules/esm-default-named/main.js
import Math, { sum } from './lib.js';
 
console.log(sum(1, 1));
console.log(Math.minus(1, 1));
 
/codes/ecma/modules/esm-default-named/main.js
import Math, { sum } from './lib.js';
 
console.log(sum(1, 1));
console.log(Math.minus(1, 1));
 

NPM

/codes/ecma/modules/esm-npm/lib.js
export const PI = 3.14;
 
export function sum(a, b) {
  return a + b;
}
 
export function minus(a, b) {
  return a - b;
}
 
export default { sum, minus, PI };
 
/codes/ecma/modules/esm-npm/lib.js
export const PI = 3.14;
 
export function sum(a, b) {
  return a + b;
}
 
export function minus(a, b) {
  return a - b;
}
 
export default { sum, minus, PI };
 
/codes/ecma/modules/esm-npm/main.js
import { sqrt } from 'mathjs';
import Math, { sum } from './lib.js';
 
console.log(sum(1, 1));
console.log(Math.minus(1, 1));
 
console.log(sqrt(16));
 
/codes/ecma/modules/esm-npm/main.js
import { sqrt } from 'mathjs';
import Math, { sum } from './lib.js';
 
console.log(sum(1, 1));
console.log(Math.minus(1, 1));
 
console.log(sqrt(16));
 
$ npm install mathjs
$ npm install mathjs
/codes/ecma/modules/esm-npm/package.json
{
  "type": "module",
  "dependencies": {
    "mathjs": "^11.7.0"
  }
}
 
/codes/ecma/modules/esm-npm/package.json
{
  "type": "module",
  "dependencies": {
    "mathjs": "^11.7.0"
  }
}
 

Editar esta página