question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"no such file or directory" when trying to read a template

See original GitHub issue

The 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:

  1. The .hbs files are not copied to the dist folder.
  2. Watch mode is not reacting to changes (creation of new templates).
  3. The path to read the template from is always weird and I cannot get it right.

Any help would be appreciated.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:8

github_iconTop GitHub Comments

6reactions
miladezzatcommented, Feb 13, 2022

I got it to work by stating the full path to template mailService like this

this.mailerService.sendMail({
  to: to,
  from: 'bla@gmail.com,
  subject: subject,
  template: path.join(process.cwd(), 'dist', 'mail', 'templates', `${template}.ejs`),
  context: {
    otp: generatedOTP,
  },
});

But I’m still looking for a way to make it work using the dir option in module.

This Works with me

2reactions
MazueraAlvarocommented, Nov 25, 2022

I managed to solve it setting the outDir to dist/src

{
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "assets": [{ "include": "config/mail/templates/**/*", "outDir": "dist/src" }],
    "watchAssets": true
  }
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found