node.js - Express and ejs: using ejs with other file extensions: require not working - Stack Overflow

According to the docs, the line:app.engine('html', require('ejs').renderFile)shoul

According to the docs, the line:

app.engine('html', require('ejs').renderFile)

should get express to process .html files as if they were .ejs which was my first simple test to have ejs work with alternative file extensions

However, when I include this in my index.js node immediately crashes with the error:

ReferenceError: require is not defined in ES module scope...

I'm using:

  • node.js : 23.1.0
  • express : 4.21.2
  • body-parser : 1.20.3
  • ejs : 3.1.10

I have done a lot of searching but can't seem to fix this, index.js looks like

    import express from "express";
    import bodyParser from "body-parser";
 
    const app = express();
    const port = 3001;
 
    app.engine('html', require('ejs').renderFile);
    // app.set('view engine', 'html')
    app.use(express.static("public"));
 
    const currentYear = new Date().getFullYear();
 
    app.use(bodyParser.urlencoded({ extended: true }));
 
    app.get("/", (req, res) => {
 
      const data = {
        title: "welcome page",
        year: currentYear,
      };
      res.render("index.html", data);
    });

    app.listen(port, () => {
      console.log(`Listening on port ${port}`);
    });

NOTE: Ok, problem is probably(?!?) as I am using type module for my project, but there should be a way around this while still keeping type module

According to the docs, the line:

app.engine('html', require('ejs').renderFile)

should get express to process .html files as if they were .ejs which was my first simple test to have ejs work with alternative file extensions

However, when I include this in my index.js node immediately crashes with the error:

ReferenceError: require is not defined in ES module scope...

I'm using:

  • node.js : 23.1.0
  • express : 4.21.2
  • body-parser : 1.20.3
  • ejs : 3.1.10

I have done a lot of searching but can't seem to fix this, index.js looks like

    import express from "express";
    import bodyParser from "body-parser";
 
    const app = express();
    const port = 3001;
 
    app.engine('html', require('ejs').renderFile);
    // app.set('view engine', 'html')
    app.use(express.static("public"));
 
    const currentYear = new Date().getFullYear();
 
    app.use(bodyParser.urlencoded({ extended: true }));
 
    app.get("/", (req, res) => {
 
      const data = {
        title: "welcome page",
        year: currentYear,
      };
      res.render("index.html", data);
    });

    app.listen(port, () => {
      console.log(`Listening on port ${port}`);
    });

NOTE: Ok, problem is probably(?!?) as I am using type module for my project, but there should be a way around this while still keeping type module

Share Improve this question edited Feb 5 at 13:09 traynor 8,9123 gold badges15 silver badges28 bronze badges asked Feb 4 at 18:04 PaulPaul 1236 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

As you're using module, just don't use require:

Try:

import ejs, {renderFile} from 'ejs'; // or just ejs + ejs.renderFile

app.engine('html', renderFile); // or ejs.renderFile

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745240932a4618179.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信