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 badges1 Answer
Reset to default 2As 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
版权声明:本文标题:node.js - Express and ejs: using ejs with other file extensions: require not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745240931a2649325.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论