admin管理员组

文章数量:1415684

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

本文标签: nodejsExpress and ejs using ejs with other file extensions require not workingStack Overflow