LoginSignup
0
0

More than 3 years have passed since last update.

Node.js(4):module PATH

Posted at

パス確認

node.jsパスモジュールはディレクトリとファイルパスが取れる。先ずは今のパスを確認する。

app.js

console.log(__dirname); //ディレクトリパス
console.log(__filename);//ファイルパス

terminal
$node app.js
/Users/xxxxx/node_practice/path
/Users/xxxxx/node_practice/path/app.js

パス重要な関数のまとめ

app.js
var path = require('path');

//今のディレクトリのパス
console.log(path.dirname(__dirname));
//ディレクトリを組み合わせ
console.log(path.join(__dirname,'/xx'));
//ファイル名
console.log(path.basename(__filename));
//ファイル拡張子
console.log(path.extname(__filename));
//パス分析
console.log(path.parse(__filename));
terminal
$node app.js

/Users/xxxxx/node_practice/path
/Users/xxxxx/node_practice/path/xx
app.js
.js
{ root: '/',
  dir: '/Users/xxxxx/node_practice/path',
  base: 'app.js',
  ext: '.js',
  name: 'app' }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0