[英]Relative path readFile error ENOENT: no such file
我有一個服務器文件,該文件根據我構建的restify
API向用戶發送電子郵件。 該文件在/lib
目錄下。 並且當該文件mail_helper.js
嘗試讀取文件./email_templates/default-inline.html
它將失敗。 它正在嘗試從節點應用程序的root
目錄而不是在lib
目錄中查找email_template
文件。 文件夾結構如下所示:
- root
- package.json
- index.js (requires lib/mail_helper.js)
- lib
- mail_helper.js (reads file ./email_templates/default-inline.html)
- email_templates
- default-inline.html
從根目錄運行node index.js
會引發錯誤:
錯誤:ENOENT:沒有此類文件或目錄,請打開“ my-local-path \\ root \\ email-templates \\ default-inline.html”
如何正確引用相對路徑以避免這種情況?
正如Keith的評論中所述,
相對路徑應這樣寫:
path.join(__dirname, 'email_templates', 'default-inline.html')
在模塊中的所有位置都將其替換后,可以解決並修復所有錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.