模組
Node.js 模組
匯出模組
// math.js
const add = (a, b) => a + b;
const subtract = (a, b) => a - b;
const multiply = (a, b) => a * b;
const divide = (a, b) => a / b;
module.exports = {add, subtract, multiply, divide}
匯出整個模組
// test.js
const math = require('./math');
console.log(math.add(3,5));
匯出部分函式
// test.js
const {add, subtract} = require('./math');
console.log(add(3,5));
console.log(subtract(3,5));
匯出函式並重新命名
// 將 v4 函式重新命名為 uuid
const { v4 : uuid } = require('uuid');
console.log(uuid());
匯入模組
import {myModule} from 'myModule'
import {Method1, Method2} from 'myModule'