본문 바로가기

언어/Node.js

[ Node.js ] path.dirname() 파일 경로에서 경로만 가져오기

반응형

Node.js에서 파일의 경로가 포함된 path에서 상위 경로만 가져오려면 path.dirname() 메서드를 사용할 수 있습니다. 이 메서드는 주어진 파일 경로에서 디렉터리 경로만 반환합니다.

다음은 그 예시입니다.

const path = require('path');

const filePath = '/home/user/documents/example.txt';
const directoryPath = path.dirname(filePath);

console.log(directoryPath); // '/home/user/documents'

위 코드에서 path.dirname()example.txt 파일 경로의 디렉터리 경로를 반환하여 파일 이름을 제외한 상위 경로만 얻을 수 있습니다.

반응형