Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8909

nodejsのpathモジュールの使い方

$
0
0

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

参考文献

この記事は以下の情報を参考にして執筆しました。


Viewing all articles
Browse latest Browse all 8909

Trending Articles