브라우저 자동화 라이브러리 Puppeteer 설치

mjml 공식사이트 화면

1. puppeteer 설치

  1. 프로젝트 디렉터리로 이동
  2. 설치환경 점검
    ####### 설치환경 점검 
    $ npm -v
    11.6.2
    
    $ node -v
    v24.12.0
    
  3. 기본 package.json 생성
    $ npm init -y
    Wrote to D:\temp\puppeteer\package.json:
    
    {
      "name": "puppeteer",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "type": "commonjs"
    }
    
  4. puppeteer 설치
    $ npm install puppeteer
    
    added 99 packages, and audited 100 packages in 14s
    
    7 packages are looking for funding
      run `npm fund` for details
    
    found 0 vulnerabilities
    

2. src/script.js 파일 생성

const puppeteer = require('puppeteer');

(async () => {
  // 브라우저 실행 (headless 모드)
  const browser = await puppeteer.launch({
    headless: true,          // false로 하면 실제 창이 뜸
    defaultViewport: null,   // 전체 화면 크기
    args: ['--no-sandbox']   // 일부 환경에서 필요
  });

  const page = await browser.newPage();

  // 페이지 이동
  await page.goto('https://public.re.kr', { waitUntil: 'networkidle2' });

  // 스크린샷 저장
  await page.screenshot({ path: 'dist/example.png' });

  // PDF 저장
  await page.pdf({ path: 'dist/example.pdf', format: 'A4' });

  // HTML 내용 가져오기
  const html = await page.content();
  console.log(html);

  await browser.close();
})();

3. 실행

$ node src/script.js

【참고자료】