node JS 톺아보기(3)

다른 js 파일에 있는 변수 가져오기


불러오고 싶은 곳에서

const { a, b } = require('./other_file1.js')
const func = require('./other_file2.js')

//사용법
func()

코드를 위처럼 쓰면 다른 파일에 선언되어 있는 a,b,func()를 가져올 수 있다. = 비구조화 할당

가져와야할 모듈 파일의 하단에 아래처럼 써서 export해주어야 한다.

//other_file1.js
module.exports = {
    a,
    b,
}
//other_file2.js
module.exports = func; //함수를 내보낼 수도 있다.


console 모듈

  • console.log()
  • console.error()
  • console.trace() : 에러 위치 추적가능
  • console.dir(객체명,{ colors : true, depth:보고싶은 객체 속성의 깊이 } : 해당 객체의 속성을 볼 수 있다.

Comments