path.join vs path.resolve

broccoli·2022년 9월 5일
0

node

목록 보기
2/2

path.join 과 path.resolve는 모두 경로를 절대경로 하나로 합쳐주는 동작을 한다.

한가지 다른점은 path.resolve는 경로를 합쳐주는 동작시 오른쪽에서 왼쪽으로 경로 merge를 진행하며 / 경로를 만날시 그 왼쪽에 있는 경로는 다 무시한다.

// # join
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');
// Returns: '/foo/bar/baz/asdf'

path.join('foo', {}, 'bar');
// Throws 'TypeError: Path must be a string. Received {}'

// # resolve
path.resolve('/foo/bar', './baz');
// Returns: '/foo/bar/baz'

path.resolve('/foo/bar', '/tmp/file/');
// Returns: '/tmp/file'

path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif');
// If the current working directory is /home/myself/node,
// this returns '/home/myself/node/wwwroot/static_files/gif/image.gif'

공통적으로 경로에 .. 가 있이면 바로 왼쪽 depth 한개를 빼고 경로가 합쳐진다.

profile
🌃브로콜리한 개발자🌟

0개의 댓글