简体   繁体   中英

Relative path readFile error ENOENT: no such file

I have a server file which sends out emails to user based on the restify API I have built. That file is under /lib directory. And when that file mail_helper.js tries to read the file ./email_templates/default-inline.html it fails. It is trying to find email_template file from the root of the node app instead of inside lib directory. The folder structure looks like this:

- 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

Running node index.js from the root directory throws the error:

Error: ENOENT: no such file or directory, open 'my-local-path\\root\\email-templates\\default-inline.html'

How can I reference relative path properly to avoid this situation?

As mentioned in the comments by Keith,

Relative path should be written like this:

path.join(__dirname, 'email_templates', 'default-inline.html')

This, worked and fix all of my errors after replacing this at all the places in my module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM