What Is Relative Path vs. Absolute Path
Create React App Tip: avoid relative import hell (../../../) in JS and TS
NodeJS Express – File path relative to root
How to use absolute path component imports in create react app | Avoiding relative path imports
Node JS Path Module | Path Module | path.resolve | path.join | path.normalize | Node JS Tutorial
EJS Path
__dirname tells you the absolute path of the directory containing the currently executing file.
By default, EJS is going to look for the file relative to process.cwd(), the directory where the Node.js process was started. __dirname is the directory that your JS file ( contact.js ) is in. If you don’t want to join to __dirname, you need to make your paths relative to where the Node.js process is started. Which means,
If folder structure is,
Root
server.js <--- EJS look for relative paths from here
views
-mail.ejs
Nodemailer & ejs - Render template from another folder
const output = await ejs.renderFile("/views/mail.ejs", {
test: 'Test'
});
EJS Path Issue 2
EJS is going to look for the file relative to process.cwd(), the directory where the Node.js process was started. __dirname is the directory that your JS file is in. If you don't want to join to __dirname, you need to make your paths relative to where the Node.js process is started.
ejs.renderFile(path.join(__dirname, 'templates/registration_confirmation.ejs'), registrationData, (err, html) => {})
ejs.renderFile can't find file if using relative pathing
Difference between these 2 methods (render,renderFile)
render() takes a string as a template and is a synchronous method.
ejs.render(str, data, options);
// => Rendered HTML string
renderFile() takes a file path as input, is asynchronous and takes a function call back.
ejs.renderFile(filename, data, options, function (err, str) {
// str => Rendered HTML string
});