Node.js requireと.load
REPLにスクリプトを読み込む
引き続き1章を読み進めている。REPL に自作のスクリプトやnpmのパッケージを読み込む方法が紹介されていた。
const hoge = require('./hoge')
require
を使うんですね。知らなかった。わかってしまえば Node だから当然か!私は、今まで .load
を使っていた。
> .help .break Sometimes you get stuck, this gets you out .clear Alias for .break .editor Enter editor mode .exit Exit the REPL .help Print this help message .load Load JS from a file into the REPL session .save Save all evaluated commands in this REPL session to a file
requireと.loadの違い
require
と.load
の違いを推測でまとめておく。
require
は Node.js の命令で、 .load
は REPLE の命令と理解した。require
は 読み込んだスクリプトで REPL** 環境を汚染するため、 delete
でキャッシュをクリアできる。
間違っていれば、教えてください。