gpt4 book ai didi

node.js - 错误 : Cannot find module 'pug'

转载 作者:IT老高 更新时间:2023-10-28 23:09:44 24 4
gpt4 key购买 nike

这是我的 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= Hello

package.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 和 jam 两个 npm 模块:

Error: Cannot find module 'pug' at Function.Module._resolveFilename (module.js:485:15) at Function.Module._load (module.js:437:25) at Module.require (module.js:513:17) at require (internal/module.js:11:18) at new View (/home/software/node_modules/express/lib/view.js:80:30) at Function.render (/home/software/node_modules/express/lib/application.js:570:12) at ServerResponse.render (/home/software/node_modules/express/lib/response.js:971:7) at /home/software/Harsh Patel/pug_with_node/index.js:8:7 at Layer.handle [as handle_request] (/home/software/node_modules/express/lib/router/layer.js:95:5) at next (/home/software/node_modules/express/lib/router/route.js:137:13)

最佳答案

尝试添加这一行

app.engine('pug', require('pug').__express)

之前

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

这为我解决了同样的问题!

关于node.js - 错误 : Cannot find module 'pug' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45342307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com