Reputation: 1097
I am trying to read html file with fs.readFile('../emailTemplates/template.html') from index.ts
my folders :
--src
--index.ts
--UsersFunctions
--UserFunctions.ts
--emailTemplates
--template.html
from index.ts :
import * as UsersFunctions from './UsersFunctions/UsersFunctions';
export const newUserRegister = UsersFunctions.onCreateNewUser;
from UsersFunctions.ts
export const onCreateNewUser = functions.firestore.document('users/allUsers/usersData/{userId}').onCreate(async (snap, context) => {
fs.readFile("../emailTemplates/template.html", {encoding: 'utf-8'}, (err, html) => {
if (err) {
console.log(err);
}
else {
}
});
}
i am getting an error from firebase functions :
Error: ENOENT: no such file or directory, open '../emailTemplates/template.html'
at Error (native)
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: 'user_code/lib/emailTemplates/newUserRegisterHtmlTemplate.html'
I just want to send an email with html template maybe there is another way to do it ? i tired to search but found nothing regarding email templates with frirebase functions with typescript
Upvotes: 3
Views: 1283
Reputation: 2024
You'll need to use Path.join(__dirname, ...)
in order to read the file from disk. Firebase Functions does properly upload the additional files surrounding your code; however the internals of Functions causes the process.CWD
info to mess up relative imports.
const templatePath = Path.join(__dirname, 'emailTemplates', 'template.html');
fs.readFile(templatePath, 'utf-8', (err, template) => {
// ...
});
Upvotes: 1
Reputation: 317712
The problem is that your path to the template doesn't match your project files organization. I suggest that you only put TypeScript source code in src
and put all other files you want to read and write under your functions
folder at the same level as src
. So, you could organize your files like this:
/functions
/src
/index.ts
/other source files
/emailTemplates
/template.html
Then, in index.ts, you could read template.html using the same path ../emailTemplates/template.html
.
Upvotes: 3