๐๐ป moment์ ๋ชจ๋์ ์ค์นํ๋ฉด npm ๋ชจ๋ ์ค์น๋ฐฉ๋ฒ์ ๋ํด์ ๋ฐฐ์ธ ์ ์์ต๋๋ค.
By installing the moment module, you can learn how to install npm modules.
๐๐ป moment๋ชจ๋์ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ์๊ฐ์ ๋ํ ๋ค์ํ ํ์์ ํํํ ์ ์๊ฒ ํด์ค๋๋ค.
The moment module allows you to represent time in various formats using JavaScript.
๐๐ป nodejs๋ฅผ ์ค์น ํ๊ณ npm๋ช
๋ น์ด๋ฅผ ํตํ์ฌ ํจํค์ง๋ฅผ ์ค์น ํ๋ฉด ์ฌ์ฉ ํ ์ ์์ต๋๋ค.
๊ธฐ๋ณธ ์ ์ธ ํจํค์ง ์ค์น ๋ช
๋ น์ด๋ ์๋์ ๊ฐ์ต๋๋ค.
You can use it by installing nodejs and installing the package through the npm command.
The basic package installation command is as follows.
# npm install moment
๐๐ป moment๋ชจ๋์ ์ฉ๋๋ JavaScript๋ก ๋ ์ง์ ์๊ฐ์ ๊ตฌ๋ฌธ ๋ถ์, ๊ฒ์ฆ, ์กฐ์ ๋ฐ ํ์๋ฅผ ํ๊ธฐ ์ํด์ ์ฌ์ฉํฉ๋๋ค.
(memonet์น์ฌ์ดํธ ์ฐธ์กฐ)
moment ๋ชจ๋์ ๊ธฐ๋ณธ์ ์ธ ์ฌ์ฉ๋ฐฉ๋ฒ์ https://momentjs.com/ ์ฒซํ์ด์ง์์ ํ์ธ ํ ์ ์์ต๋๋ค.
The purpose of the moment module is to parse, verify, manipulate, and display dates and times in JavaScript.
(Refer to memonet website)
Basic instructions for using the moment module can be found on the first page of https://momentjs.com

๐๐ป ์๋๋ moment๋ฅผ ์ฌ์ฉํ๋ sever.js ์์ ์ฝ๋ ์
๋๋ค.
Below is sever.js example code using moment.
const http = require('http');
const moment = require('moment');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('CommonJS:Hello World!\n');
});
// moment package
// usage : https://momentjs.com/
const mtime = moment().format('MMMM Do YYYY, h:mm:ss a');
const mtimes = moment().format('LT');
console.log(mtime);
console.log(mtimes);
// Start the server
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`CommonJS:Server is running on port ${PORT}`);
});
๐๐ป์คํ / Run
โ๏ธ ํฐ๋ฏธ๋ / Terminal
moment % node server.js
July 10th 2025, 02:02:12 am
02:02 AM
CommonJS:Server is running on port 3000
โ๏ธ ๋ธ๋ผ์ฐ์ / Browser
— ํฐ๋ฏธ๋์์ ์๋ฒ ์คํ ํ ๋ธ๋ผ์ฐ์ ์์ ํ์ธ ํ ์ ์์ต๋๋ค.
You can check it in your browser after running the server in the terminal.

๐๐ป ์ฝ๋ ์ค๋ช / Code Explanation
โ๏ธ package.json
— dependenciesํญ๋ชฉ์ moment๊ฐ ์์ด์ผํฉ๋๋ค.moment must be included in the dependencies section.
— npm install moment๋ฅผ ์คํํ๋ฉด momentํญ๋ชฉ์ด ์๋์ถ๊ฐ๋ฉ๋๋ค.
Running npm install moment automatically adds the moment entry.
"name": "moment",
"version": "1.0.0",
"description": "moment",
"license": "ISC",
"author": "",
"type": "commonjs",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"moment": "^2.30.1"
}
}
โ๏ธ server.js
— moment ๋ชจ๋์ ์ถ๊ฐํ๋ ๋ถ๋ถ์
๋๋ค.
This is the part where the moment module is added.
const moment = require('moment');
— ์ฌ๋ฌ๊ฐ์ง ํ์์ ์๊ฐ์ ํฐ๋ฏธ๋์ ์ถ๋ ฅํฉ๋๋ค.
It outputs the time in various formats to the terminal.
const mtime = moment().format('MMMM Do YYYY, h:mm:ss a');
const mtimes = moment().format('LT');
console.log(mtime);
console.log(mtimes);
— ๋ธ๋ผ์ฐ์ ์์ 3000๋ฒ ํฌํธ๋ก ์คํ๋ http์๋ฒ๋ฅผ ๊ตฌ์ฑํ๋ ๋ถ๋ถ์
๋๋ค.
This section configures the HTTP server to run on port 3000 in the browser.
const http = require('http');
...
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('CommonJS:Hello World!\n');
});
...
// Start the server
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`CommonJS:Server is running on port ${PORT}`);
});