Node.js - path 라이브러리

2022. 3. 15. 16:26·Node/Node.js
반응형

 

 

이번 포스팅에서는 Node.js의 내부 라이브러리인 path에 대해 알아보도록 하자.

 

< 목차 >

  1. path.join( )  /  path.resolve( )
  2. path.extname( )
  3. path.dirname( )
  4. path.basename( )

 

 

path 라이브러리는 외부 라이브러리와 다르게 npm install을 할 필요없이 다음과 같이 작성하여 바로 사용 가능하다.

const path = require('path')

해당 라이브러리의 용도에 대해 조금 언급하자면, 개발을 하는 과정에서 디렉토리를 컨트롤 해야하는 경우가 굉장히 많이 발생한다. 이 때 path 라이브러리를 사용하면 좀 더 쉽게 디렉토리 및 경로를 컨트롤 할 수 있다.

 

아래는 path 라이브러리에서 자주 사용되는 함수 위주로 정리해 본 내용이다.

 

👉 path.join( )  /  path.resolve( )

path.join( )

path.resolve( )

path.join( ) 은 디렉토리를 붙여서 경로를 만들어줄 때 사용하는 함수이다. 인자로 받은 경로들을 하나로 합쳐서 문자열 형태로 path(경로)를 리턴한다. path.resolve( ) 역시 path.join( )과 동일한 기능을 수행한다. 차이점은 resolve의 경우 인자값으로 받은 경로들을 합쳐나가는 중에 / 를 만나면 절대경로로 인식하고 나머지 경로들을 무시한다. 예제 코드를 살펴보도록 하자.

const path = require('path')

console.log(__dirname)
// output : /home/path

let dir1 = path.join(__dirname, 'bitkunst.js')
console.log(dir1)
// output : /home/path/bitkunst.js

let dir2 = path.resolve(__dirname, 'bitkunst.js')
console.log(dir2)
// output : /home/path/bitkunst.js

path.resolve( )의 경우 절대경로를 만나게 되면 절대경로를 우선시하게 된다. 

let dir3 = path.join(__dirname, '/bitkunst.js')
console.log(dir3)
// output : /home/path/bitkunst.js

let dir4 = path.resolve(__dirname, '/bitkunst.js')
console.log(dir4)
// output : /bitkunst.js

정리)

  • path.join( )  :  절대경로 무시
  • path.resolve( )  :  절대경로 존중

+ ) 참고로 위에서 사용된 __dirname은 현재 실행하는 파일의 절대경로를 나타낸다.

 

 

👉 path.extname( 'filePath' ) 

path.extname( )

path.extname( )은 파일의 확장자명을 반환하는 함수이다.

const path = require('path')

console.log(path.extname('/home/path/server.js'))
console.log(path.extname('server.js'))
// output : .js

 

 

👉 path.dirname( 'filePath' )

path.dirname( )

path.dirname( )은 파일의 디렉토리 주소를 반환하는 함수이다.

const path = require('path')

console.log(path.dirname('/home/path/server.js'))
// output : /home/path

 

 

👉 path.basename( 'filePath', path.extname('filePath') )

path.basename( )

path.basename( )은 현재 디렉토리에서 파일명을 반환하는 함수이다.

const path = require('path')

console.log(path.basename('/home/path/server.js'))
// output : server.js

 

path.basename( ) 함수의 경우 인자값 하나를 더 받을 수 있는데 인자값으로 확장자명을 넣어줄 경우 확장자명을 제외한 파일명을 반환해준다.

const path = require('path')

console.log(path.basename('/home/path/server.js', path.extname('server.js')))
// output : server

console.log(path.basename('/home/path/server.js', '.js'))
// output : server

 

 

 

 

 

 

 

반응형

'Node > Node.js' 카테고리의 다른 글

Node.js - modules (모듈)  (0) 2022.02.03
Node.js 실행하기 (with Visual Studio Code)  (0) 2022.02.02
Linux CLI 명령어 정리 (Ubuntu 사용)  (0) 2022.01.25
Node.js 설치하기 (Windows - WSL , macOS - Homebrew)  (0) 2022.01.25
Node.js 환경설정 (WSL2 설치하기)  (3) 2022.01.25
'Node/Node.js' 카테고리의 다른 글
  • Node.js - modules (모듈)
  • Node.js 실행하기 (with Visual Studio Code)
  • Linux CLI 명령어 정리 (Ubuntu 사용)
  • Node.js 설치하기 (Windows - WSL , macOS - Homebrew)
bitkunst
bitkunst
Ever tried, Ever failed, No matter. Try again, Fail again, Fail better.
  • bitkunst
    THE DEVELOPER
    bitkunst
  • 전체
    오늘
    어제
  • bitkunst GitHub
    • 분류 전체보기 (154)
      • Setup (1)
      • HTML (7)
      • CSS (10)
      • Go (2)
      • JavaScript (14)
      • TypeScript (3)
      • Node (34)
        • Node.js (7)
        • Express (19)
        • NestJS (8)
      • React (19)
      • MySQL (6)
      • BlockChain (14)
      • Ethereum (20)
      • AWS (5)
      • Linux (2)
      • Infra (2)
        • Docker (2)
      • Testing (2)
      • ABOUT CS (11)
      • 토이 프로젝트 (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    nestJS
    AWS EC2
    html
    JavaScript
    타입스크립트 블록 체인
    React
    redux
    웹프론트엔드
    TypeScript Blockchain
    블록체인 지갑 서버 만들기
    자바스크립트
    리액트
    프론트엔드
    Nest
    이더리움
    nodejs mysql
    웹
    AWS
    블록체인 트랜잭션 만들기
    블록체인
    Nodejs
    Ethereum
    Blockchain
    CSS Style
    이더리움 스마트 컨트랙트
    타입스크립트로 블록체인 만들기
    css
    타입스크립트로 블록 체인 만들기
    웹개발
    타입스크립트 블록체인 만들기
  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.1
bitkunst
Node.js - path 라이브러리
상단으로

티스토리툴바