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

Node.js 実行中のファイルの絶対パスを取得する。

$
0
0
相対パスで指定すると、実行するディクレトリによって結果が変わってしまう。 なので NodeJS のスクリプトの中でのパス指定は絶対パスを使う必要がある。 結論 __dirname を使う。 __dirname は実行中のファイルへの絶対パスを取得できる環境変数。 __dirname is an environment variable that tells you the absolute path of the directory containing the currently executing file. bin/ |_ index.js src/ のようなディレクトリ構成で、index.js から src/ を指定したい場合、 index.js const srcPath = path.resolve(dirname, '../src') と指定すると、 node bin/index.js でも、 cd bin/ && node index.js でも同じ結果になる。

Viewing all articles
Browse latest Browse all 9182

Trending Articles