【发布时间】:2016-11-18 14:01:48
【问题描述】:
我有一个服务器文件,它根据我构建的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 成功了!但是我放了那么多地方。您能否在答案中添加一些代码并解释 __dirname 的作用?
标签: javascript node.js relative-path