웹/NodeJS

[Node.js] global, console

caramel-bottle 2023. 10. 30.

js파일을 만들고 콘솔창에 아래 명령어를 입력하면 Node.js를 실행할 수 있다.

$ node [파일명]

 


global. 객체

node.js 환경에는 global. 이 생략되어있다.

global은 node.js에서 가장 최상위 객체이다.

console.log(global);

<ref *1> Object [global] {
  global: [Circular *1],
  clearImmediate: [Function: clearImmediate],
  setImmediate: [Function: setImmediate] {
    [Symbol(nodejs.util.promisify.custom)]: [Getter]
  },
  clearInterval: [Function: clearInterval],
  clearTimeout: [Function: clearTimeout],
  setInterval: [Function: setInterval],
  setTimeout: [Function: setTimeout] {
    [Symbol(nodejs.util.promisify.custom)]: [Getter]
  },
  queueMicrotask: [Function: queueMicrotask],
  structuredClone: [Getter/Setter],
  atob: [Getter/Setter],
  btoa: [Getter/Setter],
  performance: [Getter/Setter],
  navigator: [Getter],
  fetch: [Function: fetch],
  crypto: [Getter]
}


console

Node.js를 사용하기 전에는 html 파일을 만든 후에 script태그를 통해 JavaScript를 사용하고, 라이브서버나 포트번호를 통해 직접 html 파일을 열어서 출력을 확인을 해왔다.

이제 터미널에 명령어를 입력하면 바로 console 결과를 볼 수 있다.

 

console.log() 말고도 여러가지 콘솔 사용법이 존재한다. console은 개발자가 보는 출력이다. 적절하게 사용하면 편하게 코드를 작성할 수 있다.

 

  • console.log(): 개발 출력
  • console.info(): 정보 출력
  • console.wran(): 경고 출력
  • console.error(): 에러, 사용자에러, 시스템에러 출력
  • console.assert(): 조건이 만족하지 않으면 에러 메세지 출력
  • console.time() & console.timeEnd(): 같은 라벨을 사용하여 두 구문 사이에 동작 수행 시간 출력
  • console.table(): 객체 정보를 table로 출력
  • console.dir(): log와 비슷함, 보통 객체를 출력하기 좋음
  • console.count(): 호출된 횟수를 함께 출력
  • console.countReset(): 호출된 횟수를 초기화, console.count()와 라벨이 같아야함.
  • console.trace(): 함수 실행 흔적을 출력

 

 

' > NodeJS' 카테고리의 다른 글

[Node.js] this  (1) 2023.11.23
[Node.js] Node.js  (0) 2023.10.30

댓글