[英]how to get the absolute path of a file in the same the same directory as the parent folder, from the child file in nodeJS
這就是我的意思,我的文件結構如下所示:
主文件夾
應用程序.js
子文件夾填充.js
我想從file.js獲取app.js的絕對路徑
因此,假設您在file.js
並想要獲取app.js
的絕對路徑,並且child folder
已修復,您可以使用path.join()
:
const path = require( 'path' ).join( __dirname, '..', 'app.js' )
很簡單:你可以試試path.resolve
嘗試:
resolve = require('path').resolve
resolve('../../app.js')
您需要提供相對路徑,它將為您提供絕對路徑。 希望這是你正在尋找的。
怎么樣:
let path = __dirname.substr(0,__dirname.lastIndexOf('/'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.