This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let path = require('path'); | |
// 相對路徑 (隨著啟動目錄變動) | |
console.log(process.cwd()); | |
console.log(path.resolve('./')); | |
console.log(path.resolve('../')); //上一層 | |
// 絕對路徑 (依據該js檔案位置) | |
console.log(__dirname); | |
console.log(__filename); | |
console.log(path.resolve(__dirname, 'abc', 'def')); | |
console.log(path.resolve(__dirname, '../demo.js')); //上一層 | |
//讀取上一層資料夾的_utils資料夾的myRequest.js | |
const myReq = require(path.resolve(__dirname, '..', '_utils', 'myRequest.js')); |