"no such file or directory" when trying to read a template
See original GitHub issueThe same issue as this one, this issue is closed so, I’m creating a new one.
Error:
UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open '/my-awesome-project/dist/assets/templates/welcome.hbs'
package.json
{
"scripts": {
"prebuild": "rimraf dist",
"build:dev": "NODE_ENV=dev nest build",
"start": "nest start",
"start:dev": "NODE_ENV=dev nest start --watch",
},
"dependencies": {
"@nestjs-modules/mailer": "^1.5.1",
"@nestjs/common": "^7.5.1",
"@nestjs/config": "^0.6.1",
"@nestjs/core": "^7.5.1",
"@nestjs/mapped-types": "^0.1.1",
"@nestjs/platform-express": "^7.5.1",
"@nestjs/typeorm": "^7.1.5",
"dotenv": "^8.2.0",
"handlebars": "^4.7.6",
"nodemailer": "^6.4.17",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.6.3",
"typeorm": "^0.2.29"
},
"devDependencies": {
"@nestjs/cli": "^7.5.1",
"@nestjs/schematics": "^7.1.3",
"@nestjs/testing": "^7.5.1",
"@types/express": "^4.17.8",
"@types/jest": "^26.0.15",
"@types/node": "^14.14.6",
"@types/nodemailer": "^6.4.0",
"@types/supertest": "^2.0.10",
"@typescript-eslint/eslint-plugin": "^4.6.1",
"@typescript-eslint/parser": "^4.6.1",
"eslint": "^7.12.1",
"eslint-config-prettier": "7.1.0",
"eslint-plugin-prettier": "^3.1.4",
"jest": "^26.6.3",
"prettier": "^2.1.2",
"supertest": "^6.0.0",
"ts-jest": "^26.4.3",
"ts-loader": "^8.0.8",
"ts-node": "^9.0.0",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.0.5"
},
}
nest-cli.json
{
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"assets": [ // <= I receive error here: Incompatible types. Required: string. Actual: object.
{
"include": "**/*.hbs",
"watchAssets": true
}
]
}
}
application structure
|-- my-awesome-app
|-- dist
|-- env
|-- node_modules
|-- src
|-- templates
|-- account
|-- welcome.hbs
|-- transaction
|-- new-transaction.hbs
MailerOptions
createMailerOptions(): Promise<MailerOptions> | MailerOptions {
return {
transport: {
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: 'bla@gmail.com',
pass: '123456',
},
},
defaults: {
from:'"nest-modules" <modules@nestjs.com>',
},
template: {
// I have tried with all these:
// dir: __dirname + '/templates',
// dir: path.resolve(__dirname, '..', '..', 'templates'),
// dir: process.cwd() + '/templates/',
// dir: resolve(process.cwd(), 'dist/templates'),
adapter: new HandlebarsAdapter(),
options: {
strict: true,
},
},
};
}
Send email method:
sendNewAccountCreated(to: string, subject: string): Promise<any> {
return this.mailerService.sendMail({
to: to,
from: 'bla@gmail.com,
subject: subject,
template: 'welcome', // The `.hbs` extension is appended automatically.
context: { // Data to be sent to template engine.
code: 'cf1a3f828287',
username: 'john doe',
},
});
}
My issues:
- The
.hbs
files are not copied to thedist
folder. - Watch mode is not reacting to changes (creation of new templates).
- The path to read the template from is always weird and I cannot get it right.
Any help would be appreciated.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:8
Top Results From Across the Web
Try to insert template html to node-mailer but get error "ENOENT
Try to give fs.readFileSync an absolute and not relative path. As an example: fs.readFileSync(path.resolve(__dirname, 'release.hbs')).
Read more >Getting an Error | WordPress.org
... /templates/react-page-template.php): failed to open stream: No such file or directory in C:\xampp\htdocs\wp\wp-includes\template-loader.php on line 106.
Read more >Python FileNotFoundError: [Errno 2] No such file or directory ...
This error is usually raised when you use the os library. You will see an IOError if you try to read or write...
Read more >Usage - gomplate documentation
Use --in / -i if you want to set the input template right on the commandline. This overrides --file . Because of shell...
Read more >Configuring a template - copier - Read the Docs
Applying template settings (excluding files, setting arguments defaults, etc.) ... On Windows, double-quotes are not valid characters in file and directory ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This Works with me
I managed to solve it setting the
outDir
todist/src