readfiles 相對路徑 的使用標準

readfiles 相對路徑 的使用標準


非逐句翻譯
原文鏈接

first

在往服務器部署的時候readfile函數讀取相對路徑時少了一層

second

先說有用的結論吧

原文:
Using ./ worked with require but not for fs.readFileSync. That’s because for fs.readFileSync, ./ translates into the cwd (in this case /home/user/). And /home/user/files/somefile.txt does not exist.


./ refers to the current working directory, except in the require() function. When using require(), it translates ./ to the directory of the current file called. __dirname is always the directory of the current file.


翻譯:require使用相對路徑和fs.readfiles 不一樣,這是因爲fs.readfiles 會將相對路徑轉化成cwd(舉例:~~),所以,文件找不到


總結:在 require() 中使用是跟 __dirname 的效果相同,總是在當前文件的文件夾——不會因爲啓動腳本的目錄不一樣而改變,在其他情況下跟 process.cwd() 效果相同,是相對於啓動腳本所在目錄的路徑。

third

下面開始翻譯栗子
先工作,有空再碼

啦啦啦啦啦啦啦啦啦阿拉拉啊拉拉法海你不懂愛lalalala

by 中華小子 H.K


個人知識梳理,轉載請標明署名

2018年1月24日

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章