Skip to content
本页目录

node 常用内置模块

path

1.获取路径的信息

js
const filepath = '/User/why/abc.txt';
console.log(path.dirname(filepath)); // /User/why
console.log(path.basename(filepath)); // abc.txt
console.log(path.extname(filepath)); // .txt

2.join直接拼接。

js
const basepath = '../User/jing';
const filename = './abc.txt';
const filepath1 = path.join(basepath, filename);
console.log(filepath1);		// ../User/jing/abc.txt

3.resolve 会判断拼接的路径字符串中,是否有以/或./或../开头的路径。

js
const basepath2 = '/User/jingjing';
// const filename2 = '/jing/abc.txt'; 		// /jing/abc.txt
// const filename2 = './jing/abc.txt'; 		// /User/jingjing/jing/abc.txt
// const filename2 = 'jing/abc.txt'; 			// /User/jingjing/jing/abc.txt
const filename2 = '../jing/abc.txt'; 			// /User/jing/abc.txt
const result = path.resolve(basepath2, filename2);
console.log(result);

fs

MIT Licensed