[nodejs]NPM moment module

๐Ÿ‘‰๐Ÿป 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.

Browser

๐Ÿ‘‰๐Ÿป ์ฝ”๋“œ ์„ค๋ช… / 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}`);
});

Leave a Reply