这是我的index.js文件:
const express = require('express')
const app = express()
app.set('views', __dirname + '/views');
app.set('view engine', 'pug')
app.get('/', function (req, res) {
res.render('index', { title: 'Hey', message: 'Hello there!' })
})
app.listen(3333, function () {
console.log('Example app listening on port 3333!')
})index.pug文件:
html
head
title= title
body
h1= Hellopackage.json文件:
{
"name": "@npm-private/pug_with_node",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.3",
"jade": "^1.11.0",
"pug": "^2.0.0-rc.2"
}
}当我运行我的服务器文件时,它会显示一个错误。事实上,我安装了pug和jade两个npm模块:
错误:无法在需要时(内部/module.js:11:18)在Function.Module._resolveFilename (module.js:485:15)、Function.Module._load (module.js:437:25)、Module.require (module.js:513:17)上找到模块“pug”(内部/module.js:11:18)_node/express/lib/application.js:570:12)在(/home/software/node_modules/express/lib/response.js:971:7) at /home/ ServerResponse.render /ServerResponse.render/pug_with_node/index.js:8:7 at Layer.handle 请求 at next (/home/software/node_modules/express/lib/router/route.js:137:13)
发布于 2017-09-27 07:02:16
尝试添加这一行
app.engine('pug', require('pug').__express)在此之前
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');这为我解决了同样的问题!
发布于 2017-07-27 05:56:06
当全球和本地之间的模块安装不匹配时,即使安装了所有模块,您也会遇到这个问题。我建议您通过在package.json中包含依赖项来安装项目的本地所有内容。
npm install --save express jade pug发布于 2019-12-09 14:44:35
最简单的解决方法是将pug安装为开发依赖项:npm i -D pug
https://stackoverflow.com/questions/45342307
复制相似问题