pathモジュールの使い方をまとめました。
touch
コマンドでファイルを作成します。
$ touch test.js
test.js
const path = require('path')
console.log('basename:', path.basename('./dir/test.txt'))
console.log('dirname:', path.dirname('./dir/test.txt'))
console.log('extname:', path.extname('./dir/test.txt'))
console.log('parse:', path.parse('./dir/test.txt'))
console.log('join:', path.join('dir', 'dir2', 'test.txt'))
console.log('relative:', path.relative('./dir', './dir2/test.txt'))
node
コマンドを実行します。
$ node test
basename: test.txt
dirname: ./dir
extname: .txt
parse: { root: '',
dir: './dir',
base: 'test.txt',
ext: '.txt',
name: 'test' }
join: dir/dir2/test.txt
relative: ../dir2/test.txt
この記事は以下の情報を参考にして執筆しました。
パス操作(path)とファイル操作(fs)