pathモジュールの使い方をまとめました。
前提条件
- npmがインストールされていること
使い方
設定
touch
コマンドでファイルを作成します。
$ touch test.js
test.js
constpath=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
参考文献
この記事は以下の情報を参考にして執筆しました。