分割した機能ごとのjsファイルのことをモジュールと呼ぶ
importを使うのがESM方式(ECMAScript Module)で、requireを使うのがCJS(CommonJS Modules)
### import文の書き方
モジュール側
export const helloWorld = function() {
console.log('Hello World!');
}
読み込み側
import { helloWorld } from './module'
helloWorld();
### require
モジュール側
L module.exportsと書く
module.exports = function() {
console.log('hello world!');
}
読み込み側
L require文を使って先ほどのモジュールを読み込む
const helloWorldModule = require('./module.js');
helloWorldModule();
$ sudo apt install nodejs
$ node main.js
hello world!
読み込まれる方でmodule.export()とするのが鍵ですね。