簡體   English   中英

如何從nodeJS中的子文件中獲取與父文件夾相同目錄中的文件的絕對路徑

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM