ガクセイハッカソン

現役エンジニアがNode.jsを解説! 〜Node.jsのモジュール〜

2022-08-07 公開

目的

Node.jsのモジュールを理解する。

標準モジュールを使用する

const http = require("http");
const fs = require("fs");

fs.readFile("data1.txt", "utf-8", (err, data) => {
   if (err) throw err;
   console.log(data);
});

モジュールを自作する

const sayHelloWorld = () => {
  console.log("hello world!");
};
exports.sayHelloWorld1 = sayHelloWorld1;
const { sayHelloWorld1 } = require("./hello_world");
sayHelloWorld();

まとめ

今回はNode.jsのモジュールについて学習しました。次回はNode.jsのパッケージ管理について学んでいきます。

関連記事