Heroku错误-错误:无法查找视图“索引”;在views目录中 [英] Heroku Error - Error: Failed to lookup view "index" in views directory
问题描述
当部署到Heroku时,我一直收到此错误。构建过程成功完成,但是随后出现此错误。无法找出问题所在,路径不应为src / server / views吗?一切都在本地运行。
I keep getting this error when deploing to Heroku. Build process was succesfull but then I get this error. Can't figure out what is the problem and shouldn't path be src/server/views? Everything is working locally.
Error: Failed to lookup view "index" in views directory "src\server/views"
[web.1]:at /app/node_modules/express/lib/router/index.js:281:22
app[web.1]:at param (/app/node_modules/express/lib/router/index.js:354:14)
app[web.1]:at Function.render(/app/node_modules/express/lib/application.js:580:17)
app[web.1]:at param (/app/node_modules/express/lib/router/index.js:365:14)
app[web.1]:at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
app[web.1]:at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
我使用webpack tu bundle我的服务器端代码,并且使用html-webpack-plugin注入我的从views文件夹到webpack捆绑包的index.ejs模板。
I use webpack tu bundle my server side code and I use html-webpack-plugin to inject my index.ejs template from views folder to webpack bundle.
webpack.config.server.js
module.exports = {
devtool: 'source-map',
performance: {
hints: false
},
target: 'node',
node: {
__dirname: true,
__filename: true
},
entry: {
bundle: './src/server/prodServer.js'
},
output: {
path: path.join(buildPath, 'build'),
filename: '[name].js'
}
...
plugins: [
new HtmlWebpackPlugin({
template: 'ejs-loader!./src/server/views/index.ejs'
})
]
prodServer.js
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, 'views'))
文件夹结构
您可以看到我在views文件夹中有index.ejs。 prodServer是我的服务器文件,在构建webpack之后,我得到了捆绑文件和index.html。
as you can see I have index.ejs inside views folder. prodServer is my server file and after webpack build I get bundle file and index.html.
在Heroku启动脚本中,我运行节点./src/build/bundle.js
In Heroku start script I run node ./src/build/bundle.js
推荐答案
这是因为在Heroku中定义__dirname的方式。如果运行 heroku运行bash
,然后运行 pwd
,您将看到当前工作目录为 / app
,因此为什么在所有路径前都添加 / app
。
It is because of how your __dirname is defined in Heroku. If you run heroku run bash
and then run pwd
you will see that your current working directory is /app
, hence why /app
is added in front of all your paths.
尝试在bash中使用命令 ls
来查看服务器文件夹是否已列出,然后使用 cd服务器
命令查看是否还有 views
文件。请注意,它区分大小写,因此很可能已将其定义为大写。
Try to play around with the command ls
in your bash to see if your server folder is listed, and then with a cd server
command see if the views
file is there as well. Note that it is case sensitive, so most likely you had it defined in uppercase.
此外,请注意,它还会在您的计算机上添加 src\
(带有反斜杠而不是正斜杠) src\server / views
路径,该路径可能来自您的 webpack.config.server.js
中的错误配置文件。
Also, note that it adds a src\
(with a backward slash instead of forward slash) to your src\server/views
path, which could be from a wrong configuration in your webpack.config.server.js
file.
这篇关于Heroku错误-错误:无法查找视图“索引”;在views目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!