Node says Jade has no method “renderFile”, why?

后端 未结 3 570
执念已碎
执念已碎 2021-01-04 11:28

I installed jade (npm install jade) and went over to their github page to grab some examples. This is what I wanted to execute:

code.jade:

- var titl         


        
3条回答
  •  没有蜡笔的小新
    2021-01-04 12:16

    I recently came across the same issue. In the Alex Young tutorial I was following he was using jade.renderFile(). Similar to your case I was getting the same method not found message. After searching around I found that he had updated the function in a later commit. In this case he had created a custom function (renderJadeFile) as a "drop in" replacement for the jade.renderFile() function (See following code snippet).

    Hope this helps others looking for a solution to this problem.

    // Replacement function for jade.renderFile.
    function renderJadeFile(template, options) {
      var fn = jade.compile(template, options);
      return fn(options.locals);
    }
    
    emails = {
      send: function(template, mailOptions, templateOptions) {
        mailOptions.to = mailOptions.to;
        // jade.renderFile(path.join(__dirname, 'views', 'mailer', template), templateOptions, function(err, text) {
        renderJadeFile(path.join(__dirname, 'views', 'mailer', template), templateOptions, function(err, text) {
          // Add the rendered Jade template to the mailOptions
          mailOptions.body = text;
    
          // CODE SHORTENED FOR BREVETIY
      },
    
      sendWelcome: function(user) {
        this.send('welcome.jade', {
            to: user.email,
            subject: 'Welcome to Nodepad'
          },
          { locals: {
            user: user
          }
        });
      }
    };
    

提交回复
热议问题